时间函数clock()
C/C++中的计时函数是clock()
,与其相关的数据类型是clock_t
,在标准C/C++中,最小的计时单位是一毫秒.
clock_t
是一个长整形数。
typedef long clock_t
在time.h
文件中,还定义了一个常量CLOCKS_PER_SEC
,它用来表示一秒钟会有多少个时钟计时单元
#define CLOCKS_PER_SEC ((__clock_t) 1000000)
通过一个简单实例来计算一组循环需要的时间
#include "stdio.h"#include "stdlib.h"#include "time.h"main(){int i = 0;clock_t start_time;clock_t finish_time;clock_t real_time;float current_time;float program_time;start_time=clock();current_time=(float)start_time / CLOCKS_PER_SEC;printf("Program start time: %f\n",current_time);for(i=0;i<100;i++){real_time=clock();current_time=((float)real_time / CLOCKS_PER_SEC);printf("No.%d Loop Time: %f\n",i,current_time);}finish_time=clock();current_time=(float)finish_time/CLOCKS_PER_SEC;printf("Program finish time:%f\n",current_time);program_time=(float)(finish_time-start_time)/CLOCKS_PER_SEC;printf("Program complete time: %f\n",program_time);return 0;}
OutputProgram start time: 0.002459No.0 Loop Time: 0.002750No.1 Loop Time: 0.002788No.2 Loop Time: 0.002804No.3 Loop Time: 0.002819No.4 Loop Time: 0.002834No.5 Loop Time: 0.002864No.6 Loop Time: 0.002893No.7 Loop Time: 0.002922No.8 Loop Time: 0.002951No.9 Loop Time: 0.002980No.10 Loop Time: 0.002995No.11 Loop Time: 0.003025No.12 Loop Time: 0.003054No.13 Loop Time: 0.003083No.14 Loop Time: 0.003112No.15 Loop Time: 0.003166No.16 Loop Time: 0.003191No.17 Loop Time: 0.003217No.18 Loop Time: 0.003242No.19 Loop Time: 0.003267No.20 Loop Time: 0.003292No.21 Loop Time: 0.003317No.22 Loop Time: 0.003343No.23 Loop Time: 0.003368No.24 Loop Time: 0.003393No.25 Loop Time: 0.003417No.26 Loop Time: 0.003443No.27 Loop Time: 0.003467No.28 Loop Time: 0.003492No.29 Loop Time: 0.003517No.30 Loop Time: 0.003542No.31 Loop Time: 0.003568No.32 Loop Time: 0.003592No.33 Loop Time: 0.003617No.34 Loop Time: 0.003642No.35 Loop Time: 0.003668No.36 Loop Time: 0.003693No.37 Loop Time: 0.003718No.38 Loop Time: 0.003743No.39 Loop Time: 0.003768No.40 Loop Time: 0.003793No.41 Loop Time: 0.003818No.42 Loop Time: 0.003843No.43 Loop Time: 0.003868No.44 Loop Time: 0.003893No.45 Loop Time: 0.003918No.46 Loop Time: 0.003943No.47 Loop Time: 0.003968No.48 Loop Time: 0.003993No.49 Loop Time: 0.004018No.50 Loop Time: 0.004043No.51 Loop Time: 0.004068No.52 Loop Time: 0.004093No.53 Loop Time: 0.004118No.54 Loop Time: 0.004143No.55 Loop Time: 0.004168No.56 Loop Time: 0.004195No.57 Loop Time: 0.004219No.58 Loop Time: 0.004245No.59 Loop Time: 0.004270No.60 Loop Time: 0.004295No.61 Loop Time: 0.004320No.62 Loop Time: 0.004345No.63 Loop Time: 0.004370No.64 Loop Time: 0.004395No.65 Loop Time: 0.004420No.66 Loop Time: 0.004445No.67 Loop Time: 0.004470No.68 Loop Time: 0.004495No.69 Loop Time: 0.004519No.70 Loop Time: 0.004544No.71 Loop Time: 0.004569No.72 Loop Time: 0.004594No.73 Loop Time: 0.004618No.74 Loop Time: 0.004643No.75 Loop Time: 0.004669No.76 Loop Time: 0.004693No.77 Loop Time: 0.004718No.78 Loop Time: 0.004743No.79 Loop Time: 0.004768No.80 Loop Time: 0.004793No.81 Loop Time: 0.004818No.82 Loop Time: 0.004843No.83 Loop Time: 0.004868No.84 Loop Time: 0.004893No.85 Loop Time: 0.004918No.86 Loop Time: 0.004942No.87 Loop Time: 0.004967No.88 Loop Time: 0.004992No.89 Loop Time: 0.005017No.90 Loop Time: 0.005042No.91 Loop Time: 0.005067No.92 Loop Time: 0.005091No.93 Loop Time: 0.005117No.94 Loop Time: 0.005142No.95 Loop Time: 0.005167No.96 Loop Time: 0.005192No.97 Loop Time: 0.005217No.98 Loop Time: 0.005242No.99 Loop Time: 0.005267Program finish time:0.005292Program complete time: 0.002833