得到执行程序的时候我用这我需要更好的解决方案用C
clock_t start = clock();
QuickSort(0, ItemCount-1,1);
printf("Time elapsed: %f\n", ((double)clock() - start)/CLOCKS_PER_SEC);
我想在纳秒来获得时间。
得到执行程序的时候我用这我需要更好的解决方案用C
clock_t start = clock();
QuickSort(0, ItemCount-1,1);
printf("Time elapsed: %f\n", ((double)clock() - start)/CLOCKS_PER_SEC);
我想在纳秒来获得时间。
如果您使用的是类Unix系统,则很可能使用clock_gettime()
函数。在Linux中,它在-lrt
中找到,我试过的大多数其他系统都在系统C库中找到它。
应该指出的是,x86有一个非常精确的rdtsc
指令。
使用方法如下,以获得代码的执行时间在纳秒的精度struct timespec
struct timespec start, end;
clock_gettime(CLOCK_REALTIME,&start);
/* Do something */
clock_gettime(CLOCK_REALTIME,&end);
它返回一个值((((unsigned64)start.tv_sec) * ((unsigned64)(1000000000L))) + ((unsigned64)(start.tv_nsec))))
希望这个答案会更有助于你得到你所需的执行时间(以纳秒为单位)。
并使用搜索功能。这几乎每天都会问到。 – Mat 2011-03-12 19:25:42