加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

如何在Windows上测量C中的CPU时间并包含system()的调用?

发布时间:2020-12-14 04:22:40 所属栏目:Windows 来源:网络整理
导读:我想在C算法上运行一些基准测试,并希望获得所需的CPU时间,具体取决于输入.我在 Windows 7上使用Visual Studio 2012.我已经发现了一种在Windows中计算CPU时间的方法: How can I measure CPU time and wall clock time on both Linux/Windows? 但是,我在我的
我想在C算法上运行一些基准测试,并希望获得所需的CPU时间,具体取决于输入.我在 Windows 7上使用Visual Studio 2012.我已经发现了一种在Windows中计算CPU时间的方法: How can I measure CPU time and wall clock time on both Linux/Windows?

但是,我在我的算法中使用了system()命令,这不是以这种方式测量的.那么,我如何测量CPU时间并通过system()包含我的脚本调用次数?

我应该添加一个小例子.这是我的get_cpu_time函数(来自上面描述的链接):

double get_cpu_time(){
    FILETIME a,b,c,d;
    if (GetProcessTimes(GetCurrentProcess(),&a,&b,&c,&d) != 0){
        //  Returns total user time.
        //  Can be tweaked to include kernel times as well.
        return
            (double)(d.dwLowDateTime |
            ((unsigned long long)d.dwHighDateTime << 32)) * 0.0000001;
    }else{
        //  Handle error
        return 0;
    }
}

到目前为止,这工作正常,当我制作一个程序,对一些数组进行排序(或做一些其他需要一些时间的东西),它工作正常.但是,当我在这种情况下使用system() – 命令时,它不会:

int main( int argc,const char* argv[] )
{
    double start = get_cpu_time();
    double end;
    system("Bla.exe");
    end = get_cpu_time();

    printf("Everything took %f seconds of CPU time",end - start);

    std::cin.get();

}

给定的exe文件的执行以相同的方式测量并且花费大约5秒.当我通过system()运行它时,整个过程需要0秒的CPU时间,这显然不包括exe文件的执行.

一种可能性就是在系统调用上得到一个HANDLE,这有可能吗?

Linux的:

>对于挂钟时间,请使用gettimeofday()或clock_gettime()
>对于CPU时间,请使用getrusage()或times()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读