示例程序如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int result = 0, i;
struct timespec tt1, tt2;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tt1);
for (i=0; i<10; i++) {result += random();
}
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tt2);
printf("used %ld nanoseconds!\n", tt2.tv_nsec - tt1.tv_nsec);
}
编译运行如下:
$ gcc demo_clocktime.c -o demo_clocktime
$ ./demo_clocktime
used 1298 nanoseconds!
如上,此类入侵式的性能测量个别用于晚期开发。
email: MingruiZhou@outlook.com