gpt4 book ai didi

c - 隐藏在 C 中耗时

转载 作者:行者123 更新时间:2023-12-01 22:28:47 25 4
gpt4 key购买 nike

我需要在 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, &diff_keygen_sec);

我的问题是如何在一个函数中隐藏那几行,例如在“getTime”中,即:

getTime(foo(r,w,h))

最佳答案

你可以使用宏:

#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);

这将扩展到您拥有的代码中,每次在编译时使用它,并将结果绑定(bind)到 myRes

关于c - 隐藏在 C 中耗时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30558704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com