简体   繁体   English

如何使用Linux系统调用gettimeofday()来衡量C进程所花费的时间?

[英]How can I measure time taken by a process in C using Linux System Call gettimeofday()?

I'm trying to find how much time the process / function took to find the solution. 我正在尝试查找过程/功能查找解决方案所花费的时间。 I am asked to do it using gettimeofday() Linux System Call. 我被要求使用gettimeofday()Linux系统调用来做到这一点。 Any help? 有什么帮助吗? Thanks in advance. 提前致谢。

Here is my tested code snipped: 这是我经过测试的代码片段:

struct timeval t1, t2;
double elapsedTime;

// start timer
gettimeofday(&t1, NULL);

// do something
// ...

// stop timer
gettimeofday(&t2, NULL);

// compute and print the elapsed time in millisec
elapsedTime = (t2.tv_sec - t1.tv_sec) * 1000.0;      // sec to ms
elapsedTime += (t2.tv_usec - t1.tv_usec) / 1000.0;   // us to ms
fprintf(stderr, "elapsedTime = %5.3fms \n", elapsedTime);

Hope that helps. 希望能有所帮助。

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

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