1
General / Re: pollEvent slows down game loop
« on: May 19, 2019, 11:55:57 pm »Quote
I would say to start with that the timing isn't particularly accurate due to the constant output stream. Maybe try storing the values and output later, outside the loop.
I changed the code to save the timing of the event loop and the game loop in std::vectors.
std::vector<std::chrono::duration<double, std::milli>> outerLoop;
std::vector<std::chrono::duration<double, std::milli>> eventLoop;
while (window.isOpen()) {
auto start = std::chrono::system_clock::now();
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
auto eventLoopEnd = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> eventLoopDuration = eventLoopEnd - start;
eventLoop.push_back(eventLoopDuration);
window.clear();
window.display();
auto outerLoopEnd = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> duration = outerLoopEnd - start;
outerLoop.push_back(duration);
}
for (int i = 0; i < outerLoop.size(); i++) {
std::cout << eventLoop[i].count() << "/" << outerLoop[i].count() << std::endl;
}
std::vector<std::chrono::duration<double, std::milli>> eventLoop;
while (window.isOpen()) {
auto start = std::chrono::system_clock::now();
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
auto eventLoopEnd = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> eventLoopDuration = eventLoopEnd - start;
eventLoop.push_back(eventLoopDuration);
window.clear();
window.display();
auto outerLoopEnd = std::chrono::system_clock::now();
std::chrono::duration<double, std::milli> duration = outerLoopEnd - start;
outerLoop.push_back(duration);
}
for (int i = 0; i < outerLoop.size(); i++) {
std::cout << eventLoop[i].count() << "/" << outerLoop[i].count() << std::endl;
}
The result looks quite similar (time for pollEvent() / time for whole loop):
74.993/77.4701
37.7147/38.7078
61.1917/61.7517
25.2882/26.0204
27.2782/27.9917
50.2894/50.8609
6456.38/6457.17 <--
31.2891/31.9878
23.2266/23.7363
39.5351/40.2131
27.3012/27.9531
44.4714/45.216
30.5088/31.2549
22.9023/23.4643
36.3201/37.0252
46.4025/47.3068
27.0558/27.8235
51.2929/51.8331
31.0223/31.0261
37.7147/38.7078
61.1917/61.7517
25.2882/26.0204
27.2782/27.9917
50.2894/50.8609
6456.38/6457.17 <--
31.2891/31.9878
23.2266/23.7363
39.5351/40.2131
27.3012/27.9531
44.4714/45.216
30.5088/31.2549
22.9023/23.4643
36.3201/37.0252
46.4025/47.3068
27.0558/27.8235
51.2929/51.8331
31.0223/31.0261
Quote
Are you building for 32-bit or 64-bit? Is there a difference between debug and release builds?
I'm building for 64-bit on release. The debug version suffers from the same performance hit.
Quote
Have you tried it with the latest SFML version? This may have already been fixed.
Unfortunately, I was (as of yet) unable to install the newest version
Quote
If your mouse generates thousands of events for moving it, you may want to check the driver.
I measured how long the while loops takes and how many MouseMoved-Events are processed and got about 60-80 MouseMoveEvents per second.