简体   繁体   English

创建一个Timer类,应使用哪种数据类型保存值? (C / C ++)

[英]Creating a Timer class, what data type should I use to hold values? (C/C++)

I'm creating a simple timer class which returns me eg the current time in millis. 我正在创建一个简单的计时器类,例如以毫秒为单位返回当前时间。 On linux I'm using gettimeofday. 在Linux上,我正在使用gettimeofday。 I'm wondering what return type this function should have. 我想知道此函数应该具有哪种返回类型。 ie double getMillis() or uint64_t getMillis() etc... I would say uint64_t can hold bigger values and therefore recommended, though while googling I see lots of different implementations. double getMillis()uint64_t getMillis()等等...我会说uint64_t可以容纳更大的值,因此推荐使用,尽管在谷歌搜索时我看到了许多不同的实现。

Any advice on this? 有什么建议吗?

Thanks 谢谢

我建议使用毫秒表示绝对时间戳的数据类型是int64_t,主要是因为对time_t进行了签名。

I would go with the unsigned integer type since the number of milliseconds is a count. 我将使用无符号整数类型,因为毫秒数是一个计数。 Makes adding and subtracting dependable without float as well. 使加法和减法可靠,并且没有浮点数。 Most implementations I have used has unsigned integer types. 我使用的大多数实现都有无符号整数类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM