测量c ++代码的运行时间? [英] measuring the runtime of a c++ code?

查看:212
本文介绍了测量c ++代码的运行时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想测量我的c ++代码的运行时。执行我的代码需要大约12个小时,我想在我的代码执行结束时写这个时间。


解决方案

如果您使用C ++ 11,您可以使用 system_clock :: now()

  auto start = std :: chrono :: system_clock :: now(); 

/ *做一些工作* /

auto end = std :: chrono :: system_clock :: now();
auto elapsed = end - start;
std :: cout<< elapsed.count()< '\\\
';

您还可以指定用于表示持续时间的粒度:

  //使用毫秒构造一个持续时间对象
auto elapsed =
std :: chrono :: duration_cast< std :: chrono ::毫秒>(结束 - 开始);

//这使用秒构造持续时间对象
auto elapsed =
std :: chrono :: duration_cast< std :: chrono :: seconds>(end- start);



如果不能使用C ++ 11,请查看chrono from Boost。



最好的事情使用这样的标准库是它们的可移植性非常高(例如,它们都在Linux和Windows中工作)。因此,如果您决定随后移植应用程序,则不必担心太多。



这些库遵循现代C ++设计,而不是C类方法。



编辑:上述示例可用于衡量挂钟时间。但是,这不是测量程序执行时间的唯一方法。首先,我们可以区分用户和系统时间:




  • 用户时间:程序运行时间在用户空间中。

  • 系统时间:在系统(或内核)空间中运行的程序所花费的时间。执行系统调用时,程序会进入内核空间。



根据目标,可能需要或不考虑系统时间作为程序执行时间的一部分。例如,如果目标是仅仅测量用户代码上的编译器优化,那么最好省去系统时间。另一方面,如果用户想要确定系统调用是否是显着的开销,则还必须测量系统时间。



此外,由于大多数现代系统是时间共享的,不同的程序可能竞争几个计算资源(例如,CPU)。在这种情况下,可以进行另一种区分:




  • 挂钟时间通过使用挂钟时间,程序的执行方式与我们使用外部(墙)时钟的方式相同。此方法不考虑程序之间的交互。

  • CPU时间在这种情况下,我们只计算程序在CPU上实际运行的时间。如果程序(P1)与另一个(P2)共同调度,并且我们想获得P1的CPU时间,则该方法不包括P2正在运行并且P1正在等待CPU的时间(相对于
    boost.org/doc/libs/1_49_0/doc/html/chrono.html#chrono.overview.description.other_clocks\">额外时钟集:




    • process_real_cpu_clock 可捕获当前进程耗用的墙钟CPU时间。

    • process_user_cpu_clock 捕获当前进程花费的用户CPU时间。

    • process_system_cpu_clock CPU当前进程花费的时间。一个类似元组的类 process_cpu_clock 可一起捕获真实的用户CPU和系统CPU进程时间。

    • A thread_clock 线程稳定时钟,提供当前线程(由平台支持)耗用的时间。


    $ b b

    不幸的是,C ++ 11没有这样的时钟。但Boost是一个广泛使用的库,可能,这些额外的时钟将被并入C ++ 1x在某一点。因此,如果你使用Boost,你将在新的C ++标准添加它们时准备就绪。



    最后,如果你想测量程序从命令执行的时间行(与在程序中添加一些代码相反),您可以查看时间命令,就像@BЈЈЈћ建议的。但是,这种方法不会让您测量程序的各个部分(例如,执行函数所需的时间)。


    I want to measure the runtime of my c++ code. Executing my code takes about 12 hours and I want to write this time at the end of execution of my code. How can I do it in my code.

    OPerating system: Linux

    解决方案

    If you are using C++11 you can use system_clock::now():

    auto start = std::chrono::system_clock::now();
    
    /* do some work */
    
    auto end = std::chrono::system_clock::now();
    auto elapsed = end - start;
    std::cout << elapsed.count() << '\n';
    

    You can also specify the granularity to use for representing a duration:

    // this constructs a duration object using milliseconds
    auto elapsed =
        std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
    
    // this constructs a duration object using seconds
    auto elapsed =
        std::chrono::duration_cast<std::chrono::seconds>(end - start);
    

    If you cannot use C++11, then have a look at chrono from Boost.

    The best thing about using such a standard libraries is that their portability is really high (e.g., they both work in Linux and Windows). So you do not need to worry too much if you decide to port your application afterwards.

    These libraries follow a modern C++ design too, as opposed to C-like approaches.

    EDIT: The example above can be used to measure wall-clock time. That is not, however, the only way to measure the execution time of a program. First, we can distinct between user and system time:

    • User time: The time spent by the program running in user space.
    • System time: The time spent by the program running in system (or kernel) space. A program enters kernel space for instance when executing a system call.

    Depending on the objectives it may be necessary or not to consider system time as part of the execution time of a program. For instance, if the aim is to just measure a compiler optimization on the user code then it is probably better to leave out system time. On the other hand, if the user wants to determine whether system calls are a significant overhead, then it is necessary to measure system time as well.

    Moreover, since most modern systems are time-shared, different programs may compete for several computing resources (e.g., CPU). In such a case, another distinction can be made:

    • Wall-clock time: By using wall-clock time the execution of the program is measured in the same way as if we were using an external (wall) clock. This approach does not consider the interaction between programs.
    • CPU time: In this case we only count the time that a program is actually running on the CPU. If a program (P1) is co-scheduled with another one (P2), and we want to get the CPU time for P1, this approach does not include the time while P2 is running and P1 is waiting for the CPU (as opposed to the wall-clock time approach).

    For measuring CPU time, Boost includes a set of extra clocks:

    • process_real_cpu_clock, captures wall clock CPU time spent by the current process.
    • process_user_cpu_clock, captures user-CPU time spent by the current process.
    • process_system_cpu_clock, captures system-CPU time spent by the current process. A tuple-like class process_cpu_clock, that captures real, user-CPU, and system-CPU process times together.
    • A thread_clock thread steady clock giving the time spent by the current thread (when supported by a platform).

    Unfortunately, C++11 does not have such clocks. But Boost is a wide-used library and, probably, these extra clocks will be incorporated into C++1x at some point. So, if you use Boost you will be ready when the new C++ standard adds them.

    Finally, if you want to measure the time a program takes to execute from the command line (as opposed to adding some code into your program), you may have a look at the time command, just as @BЈовић suggests. This approach, however, would not let you measure individual parts of your program (e.g., the time it takes to execute a function).

    这篇关于测量c ++代码的运行时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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