• 程序运行时间测试


    https://github.com/yaowenxu/Workplace/blob/master/timer/getrusagetimer.c

    关键结构体:

    struct rusage {
                 struct timeval ru_utime; /* user time used */
                 struct timeval ru_stime; /* system time used */
                 long ru_maxrss;          /* max resident set size */
                 long ru_ixrss;           /* integral shared text memory size */
                 long ru_idrss;           /* integral unshared data size */
                 long ru_isrss;           /* integral unshared stack size */
                 long ru_minflt;          /* page reclaims */
                 long ru_majflt;          /* page faults */
                 long ru_nswap;           /* swaps */
                 long ru_inblock;         /* block input operations */
                 long ru_oublock;         /* block output operations */
                 long ru_msgsnd;          /* messages sent */
                 long ru_msgrcv;          /* messages received */
                 long ru_nsignals;        /* signals received */
                 long ru_nvcsw;           /* voluntary context switches */
                 long ru_nivcsw;          /* involuntary context switches */
         };

    程序:

    /**
     * Author: Yaowen Xu
     * Github: https://github.com/yaowenxu
     * Organization: 北航系统结构研究所
     * Date: 2019-08-18 13:31:34
     * LastEditTime: 2019-08-18 13:51:26
     * Description: 使用系统 函数 getrusage 获取程序运行相关信息 
     *              此程序主要是关注与总时间和内核空间运行时间与用户
     *              空间运行时间,使用此函数可大致对程序运行时间计算;
     * 查看: 具体使用信息可以在控制台以 man getrusage 命令查看
     */
    #include <stdio.h>
    #include <math.h>
    #include <sys/time.h>
    #include <sys/resource.h>
    
    int str2int(char* str){
        char *p = str;
        int sum = 0;
        while (*p != '')
        {
            sum = sum*10 + (*p-'0');
            p++;
        }
        return sum;
    }
    
    int main(int argc, char* argv[]){
        int def = 1000;
        if (argc == 2)
        {
            def = str2int(argv[argc-1]);
        }
        for (int i = 0; i < def ; i++)
        {
            float tmp = sqrt(i);
        }
        struct rusage usage;
        getrusage(RUSAGE_SELF, &usage);
        //getrusage(RUSAGE_CHILDREN, &usage);
        long user =  usage.ru_utime.tv_sec * 1000000 + usage.ru_utime.tv_usec; // user time used
        long sys = usage.ru_stime.tv_sec * 1e6 + usage.ru_stime.tv_usec; // sys time used
        
        printf("User: %ld us
    ", user); //  用户空间使用的时间
        printf("Sys: %ld us
    ", sys); //  内核空间使用的时间
        printf("Total: %ld us
    ", user+sys); // 总共使用的时钟
        return 0;
    }

    保持更新,如果对您有帮助请点击推荐!更多关于Linux 相关的知识,请关注 cnblogs.com/xuyaowen 

  • 相关阅读:
    sql语句3
    Android中PopupWindow的用法(位置、动画、焦点)
    JS+CSS实现的二级下拉导航菜单
    【强烈推荐】myFocustab打造的各种功能的选项卡切换
    【荐】JS+CSS实现兼容好带缓冲的动感网页右键菜单
    JS打造仿QQ的精简版折叠菜单
    JS打造类似QQ的折叠菜单
    很不错的JS+CSS滑动门
    jQuery智能判断是否是当前导航并加标记
    采用jQuery连缀写法的折叠菜单
  • 原文地址:https://www.cnblogs.com/xuyaowen/p/c-sys-getrusage.html
Copyright © 2020-2023  润新知