使用 c 语言中的 gettimeofday 函数
本文将介绍在 c 语言中使用 gettimeofday
函数计算代码块中经过的时间的几种方法。
在 c 语言中使用 gettimeofday
函数计算代码块中的经过时间
gettimeofday
函数是一个符合 posix 标准的函数,它可以检索当前时间,精度达到微秒。该函数需要两个参数,一个是 struct timeval
类型,另一个是 struct timezone
类型。虽然,timezone
结构已经贬值,应该传递 null
值来代替它。另一方面,timeval
结构包含两个成员,tv_sec
和 tv_usec
,表示自纪元以来经过的秒数和微秒数。下面的示例代码演示了我们如何测量单个函数的执行时间-loopfunc
。
一般情况下,我们需要在任何应该测量的代码块周围加上两次 gettimeofday
的调用,一次在代码块之前,另一次在代码块之后。一旦第二次调用 gettimeofday
成功返回,我们就可以使用自定义的函数-time_diff
来计算时间差。它从两个 timeval
结构中取值,并将其差值转换为秒。
#include #include #include #include
#define num 1000000
#define num2 10000000
float time_diff(struct timeval *start, struct timeval *end)
{
return (end->tv_sec - start->tv_sec) 1e-6*(end->tv_usec - start->tv_usec);
}
void loopfunc(size_t num)
{
int tmp = 0;
for (int i = 0; i < num; i) {
tmp = 1;
}
}
int main() {
struct timeval start;
struct timeval end;
gettimeofday(&start, null);
loopfunc(num);
gettimeofday(&end, null);
printf("loopfunc(%d) time spent: %0.8f sec\n",
num, time_diff(&start, &end));
gettimeofday(&start, null);
loopfunc(num2);
gettimeofday(&end, null);
printf("loopfunc(%d) time spent: %0.8f sec\n",
num2, time_diff(&start, &end));
exit(exit_success);
}
输出:
loopfunc(1000000) time spent: 0.00221000 sec
loopfunc(10000000) time spent: 0.02184500 sec
使用 clock_gettime
函数来计算 c 语言代码块中的经过时间
另外,由于 gettimeofday
已经被标记为过时了很长一段时间,建议使用 clock_gettime
函数来代替。后一个函数可以从不同的时钟中检索时序数据,第一个参数是指定的。常见的时钟类型是全系统的时钟,测量的是所谓的挂钟时间,它由 clock_realtime
宏来标识。时钟值存储在 timespec
结构中,它由两个成员组成,分别代表从 epoch 传递的秒数和纳秒数。需要注意的是,调用者事先声明了 timespec
对象,它的地址被传递给 clock_gettime
函数。
#include #include #include #include
#define num 1000000
#define num2 10000000
float time_diff2(struct timespec *start, struct timespec *end){
return (end->tv_sec - start->tv_sec) 1e-9*(end->tv_nsec - start->tv_nsec);
}
void loopfunc(size_t num)
{
int tmp = 0;
for (int i = 0; i < num; i) {
tmp = 1;
}
}
int main() {
struct timespec start2;
struct timespec end2;
clock_gettime(clock_realtime, &start2);
loopfunc(num);
clock_gettime(clock_realtime, &end2);
printf("loopfunc(%d) time spent: %0.8f sec\n",
num, time_diff2(&start2, &end2));
clock_gettime(clock_realtime, &start2);
loopfunc(num2);
clock_gettime(clock_realtime, &end2);
printf("loopfunc(%d) time spent: %0.8f sec\n",
num2, time_diff2(&start2, &end2));
exit(exit_success);
}
输出:
loopfunc(1000000) time spent: 0.00221000 sec
loopfunc(10000000) time spent: 0.02184500 sec
转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处
本文地址:
相关文章
在c中将整数转换为字符
发布时间:2024/01/03 浏览次数:131 分类:c语言
-
本教程介绍了在c中将整数转换为字符的不同方法。在c编程语言中,将整数转换为字符在各种情况下都很重要。在c中,字符是以ascii值表示的,因此转换过程相对简单。
发布时间:2023/05/07 浏览次数:364 分类:c语言
-
本文介绍了如何在 c 语言中使用 typedef enum。使用 enum 在 c 语言中定义命名整数常量 enum 关键字定义了一种叫做枚举的特殊类型。
发布时间:2023/05/07 浏览次数:129 分类:c语言
-
本文演示了如何在 c 语言中使用前缀增量与后缀增量运算符。c 语言中 i 和 i 记号的主要区别
发布时间:2023/05/07 浏览次数:275 分类:c语言
-
本文演示了如何在 c 语言中获取当前工作目录。使用 getcwd 函数获取当前工作目录的方法
发布时间:2023/05/07 浏览次数:177 分类:c语言
-
本文介绍了如何在 c 语言中使用位掩码。使用 struct 关键字在 c 语言中定义位掩码数据
发布时间:2023/05/07 浏览次数:212 分类:c语言
-
本文演示了如何在 c 语言中使用标准库排序函数。使用 qsort 函数对 c 语言中的整数数组进行排序
c 语言中的 extern 关键字
发布时间:2023/05/07 浏览次数:131 分类:c语言
-
本文介绍了如何在 c 语言中使用 extern 关键字。c 语言中使用 extern 关键字来声明一个在其他文件中定义的变量