在 c 语言中使用 getrusage 函数测量系统时间-ag捕鱼王app官网

在 c 语言中使用 getrusage 函数测量系统时间

作者:迹忆客 最近更新:2023/03/30 浏览次数:

本文将演示使用 c 语言中的 getrusage 函数测量系统时间的多种方法。


使用 getrusage 函数来测量单线程程序的系统时间

一般来说,任何正在运行的程序中都有时间的两个部分。系统时间表示程序在内核模式下执行的时间段和用户时间,表示在用户模式下经过的执行时间。这两个值的总和称为处理时间,这是优化程序性能时的有用度量。

getrusage 函数检索有关该过程的多个数据点,其中之一是表示为 struc timeval 对象的系统时间。getrusage 使用整数值和 struct rusage 对象的地址作为参数。该整数指定应测量的线程/进程,并且可以具有以下预定义的宏值 rusage_selfrusage_childrenrusage_thread

另一方面,应预先声明 rusage 结构体,成功的函数调用将在其中存储相应的值。由于 timeval 结构包含两个数据成员-秒和微秒代表时间,因此我们实现了 diffusertimediffsystemtime 函数来计算经过的时间(以秒为单位)。

#include #include #include #include 
enum {num_iters = 1000000};
void loopfunc1(size_t num)
{
    int tmp = 0;
    for (int i = 0; i < num; i) {
        tmp  = 1;
    }
}
void *loopfunc2(size_t num)
{
    for (int i = 0; i < num; i) {
        getpid();
    }
    return null;
}
float diffusertime(struct rusage *start, struct rusage *end)
{
    return (end->ru_utime.tv_sec - start->ru_utime.tv_sec) 
           1e-6*(end->ru_utime.tv_usec - start->ru_utime.tv_usec);
}
float diffsystemtime(struct rusage *start, struct rusage *end)
{
    return (end->ru_stime.tv_sec - start->ru_stime.tv_sec) 
           1e-6*(end->ru_stime.tv_usec - start->ru_stime.tv_usec);
}
int main() {
    struct rusage start, end;
    getrusage(rusage_self, &start);
    loopfunc1(num_iters);
    getrusage(rusage_self, &end);
    printf("loopfunc1 stats:\n");
    printf("  cpu time: %.06f sec user, %.06f sec system\n",
           diffusertime(&start, &end), diffsystemtime(&start, &end));
    getrusage(rusage_self, &start);
    loopfunc1(num_iters);
    getrusage(rusage_self, &end);
    printf("loopfunc2 stats:\n");
    printf("  cpu time: %.06f sec user, %.06f sec system\n",
           diffusertime(&start, &end), diffsystemtime(&start, &end));
    exit(exit_success);
}

输出:

loopfunc1 stats:
  cpu time: 0.002193 sec user, 0.000000 sec system
loopfunc2 stats:
  cpu time: 0.002087 sec user, 0.000190 sec system

使用 getrusage 函数来测量多线程程序的系统时间

getrusage 函数还可以检索调用过程中所有线程使用的系统时间。rusage_self 参数指定此函数,如上例所示,它在单线程程序中可以相互使用。

在下面的示例代码中,我们创建 16 个线程,所有这些线程执行相同的 loopfunc2 函数并终止。无论如何,getrusage 调用所检索的时间等于所有线程(包括那些创建它们的线程)总和所用的时间。同时,如果用户只想测量调用线程消耗的系统时间,则可以将 rusage_thread 作为第一个参数传递。但是请注意,rusage_thread 值是特定于 linux 的,并且必须在头文件之前定义 _gnu_source 才能包含它。

#define _gnu_source
#include #include #include #include #include 
enum {num_iters = 1000000, num_threads = 16};
void *loopfunc2(size_t num)
{
    for (int i = 0; i < num; i) {
        getpid();
    }
    return null;
}
float diffusertime(struct rusage *start, struct rusage *end)
{
    return (end->ru_utime.tv_sec - start->ru_utime.tv_sec) 
           1e-6*(end->ru_utime.tv_usec - start->ru_utime.tv_usec);
}
float diffsystemtime(struct rusage *start, struct rusage *end)
{
    return (end->ru_stime.tv_sec - start->ru_stime.tv_sec) 
           1e-6*(end->ru_stime.tv_usec - start->ru_stime.tv_usec);
}
int main() {
    struct rusage start, end;
    thrd_t threads[num_threads];
    int rc;
    getrusage(rusage_self, &start);
    for (int i = 0; i < num_threads; i) {
        rc = thrd_create(&threads[i], (thrd_start_t) loopfunc2, (void *)num_iters);
        if (rc == thrd_error) {
            perror("[error] thrd_create() call failed\n");
        }
    }
    loopfunc2(num_iters);
    getrusage(rusage_self, &end);
    printf("loopfunc2 stats:\n");
    printf("  cpu time: %.06f sec user, %.06f sec system\n",
           diffusertime(&start, &end), diffsystemtime(&start, &end));
    exit(exit_success);
}

输出:

loopfunc2 stats:
  cpu time: 0.599556 sec user, 0.233000 sec system

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在c中将整数转换为字符

发布时间:2024/01/03 浏览次数:131 分类:c语言

本教程介绍了在c中将整数转换为字符的不同方法。在c编程语言中,将整数转换为字符在各种情况下都很重要。在c中,字符是以ascii值表示的,因此转换过程相对简单。

发布时间:2023/05/07 浏览次数:364 分类:c语言

本文介绍了如何在 c 语言中使用 typedef enum。使用 enum 在 c 语言中定义命名整数常量 enum 关键字定义了一种叫做枚举的特殊类型。

c 语言中的静态变量

发布时间:2023/05/07 浏览次数:167 分类:c语言

本文介绍了如何在 c 语言中使用静态变量。在 c 语言中使用 static 变量在函数调用之间保存变量值

c 语言中生成随机数

发布时间:2023/05/07 浏览次数:158 分类:c语言

本文演示了如何在 c 语言中生成随机数。使用 rand 和 srand 函数在 c 语言中生成随机数

发布时间: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 关键字来声明一个在其他文件中定义的变量

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图