为什么一个正在运行的程序的性能随着时间的推移越来越好? [英] Why is the performance of a running program getting better over time?

查看:249
本文介绍了为什么一个正在运行的程序的性能随着时间的推移越来越好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下代码:

#include <iostream>
#include <chrono>

using Time = std::chrono::high_resolution_clock;
using us = std::chrono::microseconds;

int main()
{
    volatile int i, k;
    const int n = 1000000;

    for(k = 0; k < 200; ++k) {
            auto begin = Time::now();
            for (i = 0; i < n; ++i);  // <--
            auto end = Time::now();
            auto dur = std::chrono::duration_cast<us>(end - begin).count();
            std::cout << dur << std::endl;
    }

    return 0;
}



我反复测量内部执行时间。
结果如下图所示(y:duration,x:repeated):

I am repeatedly measuring the execution time of the inner for loop. The results are shown in the following plot (y: duration, x: repetition):

循环执行时间的减少是什么?

What is causing the decreasing of the loop execution time?

环境: linux(kernel 4.2)@ Intel i7-2600 ,编译时使用: g ++ -std = c ++ 11 main.cpp -O0 -o main

Environment: linux (kernel 4.2) @ Intel i7-2600, compiled using: g++ -std=c++11 main.cpp -O0 -o main

修改1

问题是不是关于编译器优化或性能基准
问题是,为什么性能会随着时间的推移越来越好。
我想了解运行时发生的情况。

The question is not about compiler optimization or performance benchmarks. The question is, why the performance gets better over time. I am trying to understand what is happening at run-time.

编辑2

根据Vaughn Cato 建议,我将CPU频率调整政策更改为性能。现在我得到以下结果:

As proposed by Vaughn Cato, I have changed the CPU frequency scaling policy to "Performance". Now I am getting the following results:

它确认了Vaughn Cato 的猜想。

It confirms Vaughn Cato's conjecture. Sorry for the silly question.

推荐答案

您可能看到的是CPU频率调整(调节)。当CPU不被大量使用时,CPU进入低频状态以节省功率。

What you are probably seeing is CPU frequency scaling (throttling). The CPU goes into a low-frequency state to save power when it isn't being heavily used.

在运行程序之前,CPU时钟速度可能相当低,因为没有大的负载。当您运行程序时,繁忙的循环增加了负载,CPU时钟速度上升,直到您达到最大时钟速度,减少您的时间。

Just before running your program, the CPU clock speed is probably fairly low, since there is no big load. When you run your program, the busy loop increases the load, and the CPU clock speed goes up until you hit the maximum clock speed, decreasing your times.

如果您运行您的程序连续几次,您可能会在第一次运行后看到次数保持在较低的值。

If you run your program several times in a row, you'll probably see the times stay at a lower value after the first run.

这篇关于为什么一个正在运行的程序的性能随着时间的推移越来越好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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