如何在Windows上测量C ++的CPU时间,并包括调用system()? [英] How can I measure CPU time in C++ on windows and include calls of system()?

查看:146
本文介绍了如何在Windows上测量C ++的CPU时间,并包括调用system()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在C ++算法上运行一些基准测试,并想要获得CPU所需的时间,具体取决于输入。我在Windows 7上使用Visual Studio 2012.我已经发现一种方法来计算Windows中的CPU时间:

I want to run some benchmarks on a C++ algorithm and want to get the CPU time it takes, depending on inputs. I use Visual Studio 2012 on Windows 7. I already discovered one way to calculate the CPU time in Windows: How can I measure CPU time and wall clock time on both Linux/Windows?

但是,我在我的算法中使用 system()命令,这是不是这样测量的。那么,如何测量CPU时间,并通过system()包括我的脚本调用的时间?

However, I use the system() command in my algorithm, which is not measured that way. So, how can I measure CPU time and include the times of my script calls via system()?

我应该添加一个小例子。这是我的get_cpu_time函数(从上面的链接):

I should add a small example. This is my get_cpu_time-function (From the link described above):

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()命令时,它不会:

That works fine so far, and when I made a program, that sorts some array (or does some other stuff that takes some time), it works fine. However, when I use the system()-command like in this case, it doesn't:

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文件的执行。

The execution of the given exe-file is measured in the same way and takes about 5 seconds. When I run it via system(), the whole thing takes a CPU time of 0 seconds, which obviously does not include the execution of the exe-file.

推荐答案

你也可以给外部抽样剖析器一个镜头。我使用免费的困[ http://sleepy.sourceforge.net/]and 更好的非常困[ http://www.codersnotes.com/sleepy/] 分析器

You might also give external sampling profilers a shot. I've used the freebie "Sleepy" [http://sleepy.sourceforge.net/]and even better "Very Sleepy" [http://www.codersnotes.com/sleepy/] profilers under Windows and been very happy with the results -- nicely formatted info in a few minutes with virtually no effort.

有一个类似的项目叫做Shiny[ http://sourceforge.net/projects/shinyprofiler/] 应该在Windows和*

There is a similar project called "Shiny" [http://sourceforge.net/projects/shinyprofiler/] that is supposed to work on both Windows and *nix.

这篇关于如何在Windows上测量C ++的CPU时间,并包括调用system()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆