C++中time_t和time_point的使用效率

Efficiency of using time_t and time_point in C++

在C++中,std::time_t和std::chrono::time_point是类用于存储日期和时间。使用 time_point 存储时间是否有效。貌似time_point支持的功能比较多,使用起来会不会比time_t效率低? time_point 的一个实例的内存大小大约是多少? time_t 的实例大小是多少?

Is it efficient to use time_point to store the time.

是的,它只包含一个数值。

It seems that time_point supports more functions, will it be less efficient while using it compared with time_t?

你为什么会这么想?非虚函数不会增加对象大小,简单的应该内联,这样它们就可以像直接乱用数值一样高效。

About how large in memory size is an instance of time_point?

与您告诉它使用的数字类型大小相同。如果您使用一种方便的持续时间类型,例如 seconds,则可能是 64 位。检查 sizeof 是否重要。

What's the size of instance of time_t?

未指定,通常为 32 或 64 位。检查 sizeof 是否重要。如果它是 32,那么你可能 运行 在几十年后会遇到麻烦。