小编min*_*ing的帖子

如何评估程序的运行时?

我开发了一个简单的程序,想在真机上评估它的运行时性能,例如我的 MacBook。源代码如下:

#include <stdio.h>
#include <vector>
#include <ctime>

int main () {
    auto beg = std::clock () ;
    for (int i = 0; i < 1e8; ++ i) {

    }
    auto end = std::clock () ;
    printf ("CPU time used: %lf ms\n", 1000.0*(end-beg)/CLOCKS_PER_SEC) ;
}
Run Code Online (Sandbox Code Playgroud)

它是用 gcc 编译的,优化标志设置为默认值。在 bash 脚本的帮助下,我运行了 1000 次,并通过我的 MacBook 记录了运行时间,如下所示:

[130.000000, 136.000000): 0
[136.000000, 142.000000): 1
[142.000000, 148.000000): 234
[148.000000, 154.000000): 116
[154.000000, 160.000000): 138
[160.000000, 166.000000): 318
[166.000000, 172.000000): 139
[172.000000, 178.000000): 40
[178.000000, 184.000000): …
Run Code Online (Sandbox Code Playgroud)

c++ profile

5
推荐指数
1
解决办法
155
查看次数

标签 统计

c++ ×1

profile ×1