#define GET_TIME(t, code) { t = GetMicrosecondCount();\
code; \
t = GetMicrosecondCount() - t;}
测试
#include<stdio.h>#include<stdlib.h>#include<sys/time.h>#define GET_TIME(t, code) { t = GetMicrosecondCount();\
code; \
t = GetMicrosecondCount() - t;}/**
* 获取当前时间的微妙数
*/longintGetMicrosecondCount(){struct timeval tv;gettimeofday(&tv,NULL);return tv.tv_sec*1000000+ tv.tv_usec;}/*temp定义在循环内*/unsignedlonglongfibonacci1(unsignedint count){unsignedlonglong before =0, sum =1;if(count <1){return before;}for(int i =1; i < count; i++){unsignedlonglong temp = before + sum;
before = sum;
sum = temp;}return sum;}/*temp定义在循环外*/unsignedlonglongfibonacci2(int count){unsignedlonglong before =0, sum =1;unsignedlonglong temp =0;if(count <1){return before;}for(int i =1; i < count; i++){
temp = before + sum;
before = sum;
sum = temp;}return sum;}intmain(int argc,char* argv[]){unsignedlonglong sum =0;longint t =0;//时间间隔if(argc !=2){printf("请输入次数\n");}int count =atoi(argv[1]);/*1. 将temp定义在 循环内部*/GET_TIME(t, sum =fibonacci1(count););printf("t1 = %ld, sum = %llu\n", t, sum);/*2. 将temp定义在 循环外部*/GET_TIME(t, sum =fibonacci2(count););printf("t2 = %ld, sum = %llu\n", t, sum);}