在C中经过的时间

suz*_*uzy 4 c time elapsed

#include <time.h> 
time_t start,end; 
time (&start);
//code here
time (&end); 
double dif = difftime (end,start); 
printf ("Elasped time is %.2lf seconds.", dif ); 
Run Code Online (Sandbox Code Playgroud)

我的开始和结束时间都是0.000.我不理解错误的来源.

最好还是使用time(start)和time(end)或start = clock()和end = clock()来计算经过的时间.

jwo*_*der 7

在大多数(几乎所有?)系统中,time()只有一秒的粒度,因此任何亚秒的时间长度都无法用它来测量.如果您使用的是Unix,请尝试使用gettimeofday.