#include <stdlib.h>
#include <time.h>
int main()
{
clock_t t1, t2;
t1 = clock();
sleep(2);
t2 = clock();
printf("haha %lf\n", (t2-t1)/(double)(CLOCKS_PER_SEC));
return 0;
}
我在virtualbox , t1 , t2得到的都是0...
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
int main(int argc, char * argv[]){
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
time_t t1,t2;
t1=time(NULL);
sleep(2);
t2=time(NULL);
printf("haha=%d\n",(int)difftime(t2,t1));
return 0;
}
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
struct timespec
{
__time_t tv_sec; /* Seconds. */
long tv_nsec; /* Nanoseconds. */
};
#include <time.h>
int main(void){
struct timespec start;
if( clock_gettime( CLOCK_REALTIME, &start) == -1 ) {
perror( "clock gettime" );
exit( EXIT_FAILURE );
}
}
時間轉換
https://www.epochconverter.com/
gettimeofday() should never be used to measure time: 請用clock_gettime(), 不受ntpd更新時間後, 時間不準的問題
https://blog.habets.se/2010/09/gettimeofday-should-never-be-used-to-measure-time.html
https://blog.gtwang.org/programming/measure-the-execution-time-in-c-language/2/
沒有留言:
張貼留言