重新编译后第二次运行时程序运行速度较慢

时间:2018-02-08 09:04:34

标签: c++ performance sorting qt-creator

简单程序的性能(生成created_at唯一随机混洗整数然后对它们进行排序)比较慢,当我在重新编译后第二次从#include "Snake.h" Snake::Snake(){ Snakey.setSize(sf::Vector2f(10, 10)); Snakey.setFillColor(sf::Color(0,255,0)); enter code here Snakey.setPosition(180,60); BodyList.push_back(Body); } Snake::~Snake(){ BodyList.clear(); } void Snake::Draw(sf::RenderWindow &window){ window.draw(Snakey); for (sf::RectangleShape& Body : BodyList){ window.draw(Body); } BodyList.erase(BodyList.begin()); BodyList.push_back(Body); } void Snake::Grow(){ Body.setSize(sf::Vector2f(10, 10)); Body.setFillColor(sf::Color(0,100,0)); Body.setPosition(Snakey.getPosition().x,Snakey.getPosition().y); BodyList.push_back(Body); } void Snake::Move(sf::Event event, sf::RenderWindow &window){ if (sf::Keyboard::isKeyPressed && sf::Keyboard::Left == event.key.code && Dir != "Right"){ Snakey.move(-10, 0); Body.move(-10,0); Dir = ("Left"); } else if (sf::Keyboard::isKeyPressed && sf::Keyboard::Key::Right == event.key.code && Dir != "Left"){ Snakey.move(10, 0); Body.move(10,0); Dir = ("Right"); } else if (sf::Keyboard::isKeyPressed && sf::Keyboard::Key::Up == event.key.code && Dir != "Down"){ Snakey.move(0, -10); Body.move(0,-10); Dir = ("Up"); } else if (sf::Keyboard::isKeyPressed && sf::Keyboard::Key::Down == event.key.code && Dir != "Up"){ Snakey.move(0, 10); Body.move(0,10); Dir = ("Down"); } else if (sf::Keyboard::isKeyPressed && sf::Keyboard::Key::Down == event.key.code && Dir == "Up" || sf::Keyboard::isKeyPressed && sf::Keyboard::Key::Up == event.key.code && Dir == "Down" || sf::Keyboard::isKeyPressed && sf::Keyboard::Key::Right == event.key.code && Dir == "Left"|| sf::Keyboard::isKeyPressed && sf::Keyboard::Left == event.key.code && Dir == "Right"){ window.close(); } } void Snake::TailCollision(sf::RenderWindow &window){ for (int i = 0 ; i < BodyList.size(); i++){ if ((Snakey.getPosition().x == BodyList[i].getPosition().x && Snakey.getPosition().y == BodyList[i].getPosition().y)&& (BodyList[i].getPosition().x != BodyList.back().getPosition().x && BodyList[i].getPosition().y != BodyList.back().getPosition().y)){ window.close(); } } } sf::RectangleShape Snake::GetShape(){ return Snakey; } 运行它(以及所有下一次直到下一次重新编译)

public interface IteratorWithIndex<T> extends Iterator<T> {
    int index();
}

说,第一次打印;

document.execCommand('insertImage', false, "C://issue.jpg");

然后:

1 200 000

还有另一个(还有第二个和第三个):

Qt Creator

如果我重新编译程序,那么所有重复编译。

如果我重新编译程序并从控制台运行它,那么所有输出都从#include <iostream> #include <random> #include <algorithm> #include <chrono> #include <iterator> #include <cstdint> using size_type = std::uint32_t; alignas(64) size_type v[1200000]; // behaviour really not depends on CPU affinity #ifdef __linux__ #include <sched.h> #endif int main() { #ifdef __linux__ { cpu_set_t m; int status; CPU_ZERO(&m); CPU_SET(0, &m); status = sched_setaffinity(0, sizeof(m), &m); if (status != 0) { perror("sched_setaffinity"); } } #endif std::mt19937 g(0); for (size_type i = 1; i < std::size(v); ++i) { v[i] = std::exchange(v[g() % i], i); } for (size_type i = 0; i < 10; ++i) { // first output not depends on number of iterations auto start = std::chrono::high_resolution_clock::now(); std::sort(std::begin(v), std::end(v)); std::cout << std::chrono::duration_cast< std::chrono::microseconds >(std::chrono::high_resolution_clock::now() - start).count() << std::endl; } } 附近的值开始,即使是第一个,看起来像下一个:

97896
26069
25628
25771
25863
25722
25976
25855
25687
25735

如果嘀咕很多,我在137238 35056 34880 34468 34746 27309 25781 25932 25502 25383 w / 8GB RAM,SSD上的Ubuntu Desktop 16.04.3 64位上构建并运行以上程序。没有root流行且没有调试器。我使用137648 35086 34966 26005 26305 26435 25683 25440 25981 25632 137000137207 35059 35035 34844 34563 34586 34466 34132 34327 34487

我期望反向结果,因为(可能)分支预测缓存或一些其他缓存(如果在相同代码的连续运行之间可能的话),但结果令人沮丧。

0 个答案:

没有答案