Need clear idea about timespec structure

孤街醉人 提交于 2019-12-24 03:48:04

问题


In my project I'm using struct timespec as follows

struct timespec start, end;
clock_gettime(CLOCK_REALTIME,&start);
/* Do something */
clock_gettime(CLOCK_REALTIME,&end);

It returns a value as ((((unsigned64)start.tv_sec) * ((unsigned64)(1000000000L))) + ((unsigned64)(start.tv_nsec))))

Can anyone tell why we are using the unsigned64 format and also help me understand this structure in detail?? I'm using this code in my study about time calculation in nanoseconds precision for the code execution time taken


回答1:


An unsigned 32-bit type (like unsigned int on modern platforms) have a max value of a little over four billion. If you have 5 and multiply that with one billion (like done in the code in the question) you will get a value of five billion, larger than can be contained in a 32-bit unsigned type. Enter 64-bit types, which can hold a lot higher values (18446744073709551615 to be more precise, to compare with an unsigned 32-bit max value of only 4294967295).


By the way, the code can be simplified like

start.tv_sec * 1000000000ULL + start.tv_nsec

This simplification is possible because the compiler will automatically convert lower-precision types and values to higher-precision as needed. As you have an unsigned long long (that's what the ULL means) literal value in the expression, the rest of the expression will also be converted to unsigned long long and the result will be of type unsigned long long.



来源:https://stackoverflow.com/questions/21572559/need-clear-idea-about-timespec-structure

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!