首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

求程序执行时间的函数解决方法

2012-09-27 
求程序执行时间的函数想知道一个程序运行的时间,但不知道是那个函数,求高手帮忙啊![解决办法]clock_t 和 c

求程序执行时间的函数
想知道一个程序运行的时间,但不知道是那个函数,求高手帮忙啊!

[解决办法]
clock_t 和 clock()函数(包含time.h)

clock_t time;
......
......
......
time = clock();
double start_time = (double)time;
......
time = clock();
double end_time = (double)time;

double timeSpend = end_time - start_time;
[解决办法]

C/C++ code
unsigned long startime= GetTickCount();Sleep(3000);unsigned long time=GetTickCount()-startime;printf("%d",time);
[解决办法]
C/C++ code
AME       clock - Determine processor timeSYNOPSIS       #include <time.h>       clock_t clock(void);DESCRIPTION       The clock() function returns an approximation of processor time used by the program.RETURN VALUE       The  value  returned is the CPU time used so far as a clock_t; to get the number of seconds used, divide by CLOCKS_PER_SEC.  If the processor time       used is not available or its value cannot be represent
[解决办法]
说实话,CPU时间不能拿来评价程序的real time, 多核CPU或者其他技术什么的,CPU根本不是评价标准.

建议直接获取时间差,哪一个足够精确我也不知道.

owenliang@linux-7lsl:~/csdn/src> time ./main
asdf
asdf
max_chars : 4 
0 0 0

real 0m1.528s
user 0m0.000s
sys 0m0.000s
[解决办法]
gettimeofday还不错,精确到微妙。

C/C++ code
owenliang@linux-7lsl:~/csdn/src> ./mainasdfasdfmax_chars : 4 real time : 0 secs 717 msecs owenliang@linux-7lsl:~/csdn/src> time ./mainasdfasdfasdfasdfmax_chars : 4 real time : 1 secs 344 msecs real    0m1.346suser    0m0.000ssys     0m0.000sowenliang@linux-7lsl:~/csdn/src> 

热点排行