C语言计算程序运行时间简单实例

时间函数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;
}
Output
Program start time: 0.002459
No.0 Loop Time: 0.002750
No.1 Loop Time: 0.002788
No.2 Loop Time: 0.002804
No.3 Loop Time: 0.002819
No.4 Loop Time: 0.002834
No.5 Loop Time: 0.002864
No.6 Loop Time: 0.002893
No.7 Loop Time: 0.002922
No.8 Loop Time: 0.002951
No.9 Loop Time: 0.002980
No.10 Loop Time: 0.002995
No.11 Loop Time: 0.003025
No.12 Loop Time: 0.003054
No.13 Loop Time: 0.003083
No.14 Loop Time: 0.003112
No.15 Loop Time: 0.003166
No.16 Loop Time: 0.003191
No.17 Loop Time: 0.003217
No.18 Loop Time: 0.003242
No.19 Loop Time: 0.003267
No.20 Loop Time: 0.003292
No.21 Loop Time: 0.003317
No.22 Loop Time: 0.003343
No.23 Loop Time: 0.003368
No.24 Loop Time: 0.003393
No.25 Loop Time: 0.003417
No.26 Loop Time: 0.003443
No.27 Loop Time: 0.003467
No.28 Loop Time: 0.003492
No.29 Loop Time: 0.003517
No.30 Loop Time: 0.003542
No.31 Loop Time: 0.003568
No.32 Loop Time: 0.003592
No.33 Loop Time: 0.003617
No.34 Loop Time: 0.003642
No.35 Loop Time: 0.003668
No.36 Loop Time: 0.003693
No.37 Loop Time: 0.003718
No.38 Loop Time: 0.003743
No.39 Loop Time: 0.003768
No.40 Loop Time: 0.003793
No.41 Loop Time: 0.003818
No.42 Loop Time: 0.003843
No.43 Loop Time: 0.003868
No.44 Loop Time: 0.003893
No.45 Loop Time: 0.003918
No.46 Loop Time: 0.003943
No.47 Loop Time: 0.003968
No.48 Loop Time: 0.003993
No.49 Loop Time: 0.004018
No.50 Loop Time: 0.004043
No.51 Loop Time: 0.004068
No.52 Loop Time: 0.004093
No.53 Loop Time: 0.004118
No.54 Loop Time: 0.004143
No.55 Loop Time: 0.004168
No.56 Loop Time: 0.004195
No.57 Loop Time: 0.004219
No.58 Loop Time: 0.004245
No.59 Loop Time: 0.004270
No.60 Loop Time: 0.004295
No.61 Loop Time: 0.004320
No.62 Loop Time: 0.004345
No.63 Loop Time: 0.004370
No.64 Loop Time: 0.004395
No.65 Loop Time: 0.004420
No.66 Loop Time: 0.004445
No.67 Loop Time: 0.004470
No.68 Loop Time: 0.004495
No.69 Loop Time: 0.004519
No.70 Loop Time: 0.004544
No.71 Loop Time: 0.004569
No.72 Loop Time: 0.004594
No.73 Loop Time: 0.004618
No.74 Loop Time: 0.004643
No.75 Loop Time: 0.004669
No.76 Loop Time: 0.004693
No.77 Loop Time: 0.004718
No.78 Loop Time: 0.004743
No.79 Loop Time: 0.004768
No.80 Loop Time: 0.004793
No.81 Loop Time: 0.004818
No.82 Loop Time: 0.004843
No.83 Loop Time: 0.004868
No.84 Loop Time: 0.004893
No.85 Loop Time: 0.004918
No.86 Loop Time: 0.004942
No.87 Loop Time: 0.004967
No.88 Loop Time: 0.004992
No.89 Loop Time: 0.005017
No.90 Loop Time: 0.005042
No.91 Loop Time: 0.005067
No.92 Loop Time: 0.005091
No.93 Loop Time: 0.005117
No.94 Loop Time: 0.005142
No.95 Loop Time: 0.005167
No.96 Loop Time: 0.005192
No.97 Loop Time: 0.005217
No.98 Loop Time: 0.005242
No.99 Loop Time: 0.005267
Program finish time:0.005292
Program complete time: 0.002833


你可能感兴趣的:(C语言小练习,c语言,c++,时间函数,时钟)