在C语言中隐藏经过的时间
我需要隐藏指令以获取C中的运行时间。例如,在下一个代码中,有几行指令用于获取函数foo的运行时间在C语言中隐藏经过的时间,c,C,我需要隐藏指令以获取C中的运行时间。例如,在下一个代码中,有几行指令用于获取函数foo的运行时间 struct timeval start_keygen, end_keygen; long int diff_keygen_sec = 0; gettimeofday(&start_keygen, NULL); foo(r, w, h); gettimeofday(&end_keygen, NULL); timediff(start_keygen, end_keygen, &
struct timeval start_keygen, end_keygen;
long int diff_keygen_sec = 0;
gettimeofday(&start_keygen, NULL);
foo(r, w, h);
gettimeofday(&end_keygen, NULL);
timediff(start_keygen, end_keygen, &diff_keygen_sec);
我的问题是如何在一个函数中隐藏这几行,例如在“getTime”中,即:
您可以使用宏:
#define TIME(e, res) do{struct timeval start_keygen, end_keygen; \
res = 0; \
gettimeofday(&start_keygen, NULL); \
e; \
gettimeofday(&end_keygen, NULL); \
timediff(start_keygen, end_keygen, &res)} while(0) \
然后你可以做:
long int myRes;
TIME(foo(r,w,h), myRes);
每次在编译时使用它时,它都会扩展到您拥有的代码中,并将结果绑定到
myRes
宏确实是您想要的,但是您可以不传递函数调用,而是让它有点不同,它在语法上与函数类似,不那么难看
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#define TimedExecution(elapsed, function, ...) \
do { \
struct timeval start; \
struct timeval end; \
gettimeofday(&start, NULL); \
function(__VA_ARGS__); \
gettimeofday(&end, NULL); \
*((long int *) elapsed) = timevaldiff(&start, &end); \
} while (0)
long int
timevaldiff(struct timeval *starttime, struct timeval *finishtime)
{
long int msec;
msec = (finishtime->tv_sec - starttime->tv_sec ) * 1000;
msec += (finishtime->tv_usec - starttime->tv_usec) / 1000;
return msec;
}
void
execute(const char *message)
{
for (int i = 0 ; i < 3 ; ++i)
{
fprintf(stdout, "%s\n", message);
sleep(1);
}
}
int
main(void)
{
long int elapsed;
TimedExecution(&elapsed, execute, "Hello World!");
fprintf(stdout, "Executed in: %ld msec\n", elapsed);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#定义TimedExecution(已用时间、函数等)\
做{\
结构timeval启动\
结构timeval-end\
gettimeofday(&start,NULL)\
函数(变量参数)\
gettimeofday(&end,NULL)\
*((长整型*)经过)=timevaldiff(&start,&end)\
}而(0)
长整型
timevaldiff(结构timeval*starttime,结构timeval*finishtime)
{
长整数毫秒;
毫秒=(完成时间->电视秒-开始时间->电视秒)*1000;
毫秒+=(完成时间->电视使用时间-开始时间->电视使用时间)/1000;
返回毫秒;
}
无效的
执行(常量字符*消息)
{
对于(int i=0;i<3;++i)
{
fprintf(标准输出,“%s\n”,消息);
睡眠(1);
}
}
int
主(空)
{
漫长的过去;
TimedExecution(&已过,执行“helloworld!”);
fprintf(标准输出,“在:%ld毫秒内执行”,已过);
返回0;
}
虽然很难看,但是宏怎么样?@m.s.它并不难看!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#define TimedExecution(elapsed, function, ...) \
do { \
struct timeval start; \
struct timeval end; \
gettimeofday(&start, NULL); \
function(__VA_ARGS__); \
gettimeofday(&end, NULL); \
*((long int *) elapsed) = timevaldiff(&start, &end); \
} while (0)
long int
timevaldiff(struct timeval *starttime, struct timeval *finishtime)
{
long int msec;
msec = (finishtime->tv_sec - starttime->tv_sec ) * 1000;
msec += (finishtime->tv_usec - starttime->tv_usec) / 1000;
return msec;
}
void
execute(const char *message)
{
for (int i = 0 ; i < 3 ; ++i)
{
fprintf(stdout, "%s\n", message);
sleep(1);
}
}
int
main(void)
{
long int elapsed;
TimedExecution(&elapsed, execute, "Hello World!");
fprintf(stdout, "Executed in: %ld msec\n", elapsed);
return 0;
}