在 unix 中获取本地时间的最快方法
Fastest way to get local time in unix
我正在研究Unix系统上的日志记录性能。问题是我发现最昂贵的操作是获取日期。
目前我有这样的时间——
timeb currTime;
struct tm localTime;
ftime( &currTime )
localtime_r(&currTime.time, &localTime);
我把它改成了
struct timeval tv;
gettimeofday(&tv, NULL);
time_t curtime = tv.tv_sec;
struct tm localTime = *(localtime(&curtime));
并且没有看到任何性能改进。所以我想知道也许这不是在 unix 系统上获取本地时间的最快方法?
请分享你对此的看法。
提前感谢。
gettimeofday可以非常快速地为您提供UTC时间。 转换为本地时间不是那么快。
将其转换为本地时间,直到您脱离性能关键路径。
此外,您还可以每天一次缓存 UTC 到本地时间转换因子,以加快到本地时间的转换速度,尽管这并不完美,但对于日志记录目的来说可能已经足够了。
相关文章:
- C++为构建时间获取QDateTime的可靠方法
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 将时间戳打印到流的最简单方法
- 通常使用什么方法来检测时间情况?
- 在C++中执行 N 阶乘编译时间的 3 种不同/相同方法
- 有没有更准确的方法来记录C++时间?
- 在给定程序中降低矢量数组实现堆栈的时间复杂度有哪些不同的可能方法?
- 更好的单元测试方法,花费的时间太长
- 替换枚举以最大化编译时间检查的最佳方法
- 如何在长时间运行的方法中等待信号?
- 使用 GetLastError() 的真正正确的方法(时间)是什么?
- 为什么以相同的数量插入到集合中,基于不同的方法具有不同的运行时间?
- C 类方法的执行时间取决于什么
- 扩展中的日期时间对象方法C++问题
- 一定时间后强制终止方法
- 此插入方法的时间和空间复杂性
- 如何恢复注册表项的所有元素?(上次写入时间,类型,值,名称...这是正确的方法吗?
- 查找系统调用需要多长时间的可能方法
- 在 C++11 中表示日内时间 HH:MM:SS 的最佳方法是什么?
- C 编译时间检查模板类型中是否存在方法