我在网上搜索过但我只找到了一种方法,但是这样它就会以秒为单位而不是几毫秒.
我的代码是:
#include <stdio.h>
#include <assert.h>
#include <time.h>
int main(void)
{
int solucion;
time_t start,stop;
clock_t ticks;
long count;
time(&start);
solucion = divisores_it(92000000,2);
time(&stop);
printf("Finnished in %f seconds. \n",difftime(stop,start));
return 0;
}
解决方法
跨平台的方式是使用ftime.
Windows特定链接:http://msdn.microsoft.com/en-us/library/aa297926(v=vs.60).aspx
以下示例.
#include <stdio.h>
#include <sys\timeb.h>
int main()
{
struct timeb start,end;
int diff;
int i = 0;
ftime(&start);
while(i++ < 999) {
/* do something which takes some time */
printf(".");
}
ftime(&end);
diff = (int) (1000.0 * (end.time - start.time)
+ (end.millitm - start.millitm));
printf("\noperation took %u milliseconds\n",diff);
return 0;
}
我运行上面的代码并使用VS2008跟踪它,并看到它实际上调用了Windows GetSystemTimeAsFileTime函数.
无论如何,ftime会给你几毫秒的精度.