我想测量挂钟时间,clock_gettime
但每次运行代码时,它都显示为 0。这是为什么呢?(我希望我的结果以毫秒为单位。)
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
int main( int argc, char **argv )
{
struct timespec start, stop;
unsigned long accum;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
perror( "clock gettime" );
exit( EXIT_FAILURE );
}
int i;
for(i=0; i<100000000; i++){int a = 3; int b = 100; int c = a*b;}
//system( argv[1] );
if( clock_gettime( CLOCK_REALTIME, &stop) == -1 ) {
perror( "clock gettime" );
exit( EXIT_FAILURE );
}
accum = (unsigned long)( (stop.tv_sec - start.tv_sec) * 1000) + (unsigned long)( (stop.tv_nsec - start.tv_nsec) / 1000000 ) +0.5;
printf( "%lu\n", accum );
return( EXIT_SUCCESS );
}