如何跟踪我的程序执行某个指令的时间,例如,如果要测量磁盘,需要时间,最后我的程序应该按照< / p>
real = 50.0s user = 30.s sys = 20.0s
如何区分和收集系统时间以及实际命令时间?
答案 0 :(得分:0)
在我过去使用pureCoverage来检查执行时间的那一天,但它很昂贵。可能有免费的工具可以做同样的事情,但对于快速而肮脏的解决方案......
include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main(void) {
time_t start_time, end_time;
start_time = time(NULL);
//Do your thing...
end_time = time(NULL);
int diff = end_time - start_time;
//Diff will be the number of milliseconds that elapsed.
}
答案 1 :(得分:0)
在Linux中使用getrusage
:
#include <sys/time.h>
#include <sys/resource.h>
...
struct rusage rusage;
getrusage(RUSAGE_SELF, &rusage);
/* use rusage.ru_utime and rusage.ru_stime for user and system time resp. */
答案 2 :(得分:0)
还有另一种解决方案,与前一个例子没什么不同。
#include<time.h>
int main ()
{
clock_t start_time, stop_time;
start_time = clock();
// ... do your thing;
stop_time = clock();
printf("Elapsed time: %lf", (double)( stop_time - start_time ) / CLOCKS_PER_SEC);
}