龙空技术网

C语言库函数之clock()详解

编程基础知识分享 212

前言:

现在大家对“c语言电子钟”大致比较讲究,大家都想要分析一些“c语言电子钟”的相关文章。那么小编在网摘上汇集了一些有关“c语言电子钟””的相关资讯,希望姐妹们能喜欢,朋友们一起来了解一下吧!

原函数:

clock_t clock(void)

参数:

NA

返回值:

函数返回程序启动以来经过的时钟滴答数。失败时,函数返回值-1。

如何使用clock() 函数:

#include <time.h>

#include <stdio.h>

int main() {

clock_t start_t, end_t, total_t;

int i;

start_t = clock();

printf("Line 1 - Starting of the program, start_t = %ld ", start_t);

printf("Line 2 - Going to scan a big loop, start_t = %ld ", start_t);

for(i=0; i< 100000000; i++) {

}

end_t = clock();

printf("Line 3 - End of the big loop, end_t = %ld ", end_t);

total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;

printf("Line 4 - Total time taken by CPU: %f ", total_t );

printf("Line 5 - Exiting of the program... ");

return(0);

}

编译和运行上面的程序,产生如下结果:

Line 1 - Starting of the program, start_t = 0

Line 2 - Going to scan a big loop, start_t = 0

Line 3 - End of the big loop, end_t = 400000

Line 4 - Total time taken by CPU: 0.000000

Line 5 - Exiting of the program...

标签: #c语言电子钟