代码执行时间计算(宏和函数的执行时间差别)

#define INFORMATION \
	printf("行数:%d---位置:%s---日期:%s---时间:%s\n",\
	__LINE__, __FILE__, __DATE__, __TIME__)

#define name_cat(str1,str2) str1##str2

#define	STC(NAME) printf("本次输出"#NAME"的值为%d\n",NAME)

#define TEST(a) a = 1 + 2 + 3 + 4 + 7 + 55 + 1 * 48 * 35 * 12 * 100000

int main(){
	int test(int);
	int i = 0, aabb = i;
	clock_t start, finish;
	start = clock();
	for (i = 0; i < 10000; i++){
		//printf("%3d", i);
		TEST(aabb);
	}
	finish = clock();
	printf("\n宏所执行的时间为%f。\n", (double)(finish - start));
	start = clock();
	for (i = 0; i < 10000; i++){
		//printf("%3d",i);
		test(aabb);
	}
	finish = clock();
	printf("\n函数所执行的时间为%f。\n", (double)(finish - start));
	system("pause");
	return 0;
}

int test(int aabb){
	aabb = 1 + 2 + 3 + 4 + 7 + 55 + 1 * 48 * 35 * 12 * 100000;
	return aabb;
}

你可能感兴趣的:(C/C++)