c语言测试代码运行时间


c语言实现测试代码运行时间:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main(){
	clock_t run_start,run_finish;
	long i = 1000000000L;
	run_start = clock();
	while(i--);
	run_finish = clock();
	double run_time = (double)(run_finish - run_start)/CLOCKS_PER_SEC*1000;
	printf("run time is %fms\n",run_time);
	return 0;
}

clock_t, clock()定义于time.h中,clock()返回从程序运行时刻开始的时钟周期数,类型为long.CLOCKS_PER_SEC定义了每秒钟包含多少了时钟单元数,因为计算ms,所以乘以1000

你可能感兴趣的:(c)