最近在看《算法:C语言实现(第1~4部分)》,在第二章有一个小程序,是计算从1到1000000000所花的时间。这个程序很简单,只需要3个简单的for循环就实现了,但怎样计算程序运行的时间却让我犯了难,似乎以前还没有这样干过。在网上查找了资料以后,整理的程序实现如下。
/*===========================================================*
* 函数功能:计算从1数到1000000000所花的时间
*===========================================================*/
/*===========================================================
* 在time.h文件中有如下定义
* #ifndef _CLOCK_T_DEFINED
* typedef long clock_t;
* #define _CLOCK_T_DEFINED
* #endif
* 宏CLOCKS_PER_SEC:表示一秒钟包含的时钟单元的数量
*==========================================================*/
#include <stdio.h>
#include <time.h>
#define N 1000
int shushu()
{
int i,j,k,count = 0;
for(i = 0; i < N; i++)
for(j = 0; j < N ; j++)
for(k = 0; k < N; k++)
count++;
return count;
}
int main(void)
{
int sum;
clock_t start,finish;
double tota