我想要测量两个事件之间的时间持续时间,比如在不同时间点捕获CPU快照。
在将其乘以
1000000000L
之后,是否需要进行float
转换?这样做正确吗?float get_elapsed(domain_info *dom) {
float temp;
temp = (float)((dom->t2.tv_sec - dom->t1.tv_sec) * 1000000000L + (dom->t2.tv_nsec - dom->t1.tv_nsec);
return temp;
}
float
来表示浮点类型。 - undefinedstruct timeval
的POSIX定义(我认为t1
和t2
是这样的),那么它们都是整数,结果也是整数。整数结果可以隐式转换为浮点数值(小数部分为零)。因此,不需要显式转换。实际上,也不需要使用浮点数类型。 - undefinedstruct timespec
而不是struct timeval
。 - undefinedconst struct timespec *
,而不是domain_info
是什么。这样可以记录实际的数据依赖关系,并且您可以得到一个在其他情境中也有用的更通用的函数。 - undefined