我正在尝试使用clock_gettime比较函数调用,但我得到了奇怪的结果。
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <stdlib.h>
int myppid(){
int myarray[] = {1,2};
return(myarray[1]);
}
main(int argc, char **argv){
uint64_t diff,diff1,diff2;
struct timespec start,end;
int billion = 1000000000;
int i;
/*** for loop to find average overhead ***/
clock_gettime(CLOCK_MONOTONIC, &start); //start clock
for (i; i<1000000; i++); //waste time
clock_gettime(CLOCK_MONOTONIC, &end); //end clock
diff = billion * ( end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
printf("elapsed time of empty for loop: %llu nanoseconds\n", (long long unsigned int) diff);
/*** myppid to find average overhead of a local function ****/
clock_gettime(CLOCK_MONOTONIC, &start); //start clock
for (i; i<1000000; i++) //waste time
{myppid();}
clock_gettime(CLOCK_MONOTONIC, &end); //end clock
diff1 = billion * ( end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
//diff1 = diff1 - diff;
printf("elapsed time of for loop of myppid: %llu nanoseconds\n", (long long unsigned int) diff1);
// getppid.c to find average overhead of the system call
clock_gettime(CLOCK_MONOTONIC, &start); //start clock
for (i; i<1000000; i++) //waste time
{getppid();}
clock_gettime(CLOCK_MONOTONIC, &end); //end clock
diff2 = billion * ( end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
//diff2 = diff2 - diff1;
printf("elapsed time of for loop of getppid: %llu nanoseconds\n", (long long unsigned int) diff2);
exit(0);
}
我除了在纳秒内增加时钟数,但我得到:
为空循环的经过时间:421纳秒
for myppid循环的经过时间:160纳秒
getppid循环的经过时间:195纳秒
答案 0 :(得分:0)
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>
#include <stdint.h>
#include <stdlib.h>
int myppid(){
int myarray[] = {1,2};
return(myarray[1]);
}
main(int argc, char **argv){
uint64_t diff,diff1,diff2;
struct timespec start,end;
int billion = 1000000000;
int i;
// getppid.c to find average overhead of the system call
clock_gettime(CLOCK_MONOTONIC, &start); //start clock
for (i; i<1000000; i++) //waste time
{getppid();}
clock_gettime(CLOCK_MONOTONIC, &end); //end clock
diff2 = billion * ( end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
//diff2 = diff2 - diff1;
printf("elapsed time of for loop of getppid: %llu nanoseconds\n", (long long unsigned int) diff2);
/*** myppid to find average overhead of a local function ****/
clock_gettime(CLOCK_MONOTONIC, &start); //start clock
for (i; i<1000000; i++) //waste time
{myppid();}
clock_gettime(CLOCK_MONOTONIC, &end); //end clock
diff1 = billion * ( end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
//diff1 = diff1 - diff;
printf("elapsed time of for loop of myppid: %llu nanoseconds\n", (long long unsigned int) diff1);
/*** for loop to find average overhead ***/
clock_gettime(CLOCK_MONOTONIC, &start); //start clock
for (i; i<1000000; i++); //waste time
clock_gettime(CLOCK_MONOTONIC, &end); //end clock
diff = billion * ( end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
printf("elapsed time of empty for loop: %llu nanoseconds\n", (long long unsigned int) diff);
exit(0);
}
令人惊讶的是,此代码还会为您提供递减的时间值顺序。 但如果你访问this link,那就不那么令人惊讶了。它是一种编译器优化技术。