在这部分代码中,我尝试使用 getrusage 来测量时间:
getrusage(RUSAGE_SELF, &usage2);
start2 = usage2.ru_stime;
int counter;
for (counter = 0; counter<10000; counter++) {
int j;
int found_elements = 0;
for (j = b; j < c; j++) {
Node *current = NULL;
HASH_FIND_INT(mainhash,&j,current);
if (current) {
found_elements++;
}
}
}
getrusage(RUSAGE_SELF, &usage2);
end2 = usage2.ru_stime;
printf("%.8f", (double)((end2.tv_sec - start2.tv_sec) + (end2.tv_usec - start2.tv_usec)));
用这些:
struct rusage usage1, usage2;
struct timeval start1, end1, start2, end2;
但它在终端上给了我这个:
112001.00000000
我知道这不是真的。我需要格式为 5.54676686(秒)的经过时间。我怎样才能正确地得到它?