C++ std::chrono really slow in Qt
-
Hello everyone this is my first post.
I write because I try to make a real time simulation in Qt C++ and I have the following issue:
I am using the code below to see in console the real time counting up. From Visual Code Studio it works fine and smooth, but in Qt it is really slow (takes several minutes per second)...
#include <iostream> #include <chrono> int main(int argc, char *argv[]) { auto startTime = std::chrono::system_clock::now(); while (true) { auto currentTime = std::chrono::system_clock::now(); std::chrono::duration<double> elapsedTime = currentTime - startTime; std::cout << elapsedTime.count() << std::endl; } return 0; }
The code is the same, what can be the problem?
Thanks
-
Hello everyone this is my first post.
I write because I try to make a real time simulation in Qt C++ and I have the following issue:
I am using the code below to see in console the real time counting up. From Visual Code Studio it works fine and smooth, but in Qt it is really slow (takes several minutes per second)...
#include <iostream> #include <chrono> int main(int argc, char *argv[]) { auto startTime = std::chrono::system_clock::now(); while (true) { auto currentTime = std::chrono::system_clock::now(); std::chrono::duration<double> elapsedTime = currentTime - startTime; std::cout << elapsedTime.count() << std::endl; } return 0; }
The code is the same, what can be the problem?
Thanks
@parysudo said in C++ std::chrono really slow in Qt:
From Visual Code Studio it works fine and smooth, but in Qt it is really slow
Visual Code Studio is an IDE. Qt is not. Nor is it a language. Nor does it have anything to do with how C++
std::...
stuff works, includingstd::chrono
.When run outside an IDE your code should take the same amount of time regardless of how you built it, with or without Qt (it does not use any Qt calls).
So do you mean when running inside the Qt Creator IDE? Then it probably comes down to how long the
std::cout
takes in each IDE to display loads of text very fast in its "stdout output window". Which might differ from one IDE to another.If you comment out that line you will discover
std::chrono
cannot be "slow".This code tests nothing other than that, I suggest you look at a more realistic code if you are trying to measure something.
-
Hello everyone this is my first post.
I write because I try to make a real time simulation in Qt C++ and I have the following issue:
I am using the code below to see in console the real time counting up. From Visual Code Studio it works fine and smooth, but in Qt it is really slow (takes several minutes per second)...
#include <iostream> #include <chrono> int main(int argc, char *argv[]) { auto startTime = std::chrono::system_clock::now(); while (true) { auto currentTime = std::chrono::system_clock::now(); std::chrono::duration<double> elapsedTime = currentTime - startTime; std::cout << elapsedTime.count() << std::endl; } return 0; }
The code is the same, what can be the problem?
Thanks
Hi, welcome to the forum.
Qt is a library and you're not using it here. Do you mean Qt Creator (the IDE)?
What you're doing here is basically stress testing the standard stream throughput of the thing that redirects it. It has nothing to do with chrono. You might as well just do
while(true) std::cout << "Hello" << std::endl;
cout is a stream that gets redirected by the IDE (either VS Code or Creator) to its window. It's buffered and issuing
std::endl
flushes that buffer i.e. it's a blocking operation that waits until the receiver gets (and potentially processes) all the data in the stream.what can be the problem?
There is no problem. Pumping megabytes of formatted text non-stop through standard stream is not something it is designed for. What you're seeing is just how fast a given IDE is able to process that amount of stream data.
-
@parysudo said in C++ std::chrono really slow in Qt:
From Visual Code Studio it works fine and smooth, but in Qt it is really slow
Visual Code Studio is an IDE. Qt is not. Nor is it a language. Nor does it have anything to do with how C++
std::...
stuff works, includingstd::chrono
.When run outside an IDE your code should take the same amount of time regardless of how you built it, with or without Qt (it does not use any Qt calls).
So do you mean when running inside the Qt Creator IDE? Then it probably comes down to how long the
std::cout
takes in each IDE to display loads of text very fast in its "stdout output window". Which might differ from one IDE to another.If you comment out that line you will discover
std::chrono
cannot be "slow".This code tests nothing other than that, I suggest you look at a more realistic code if you are trying to measure something.
-
-
@JonB Yeah my apologies I mean Qt Creator. And I will try to test it with no cout to see. Thanks!
@parysudo By the way - when testing any sort of performance or timing don't put I/O operations in the thing you test i.e. reading/writing a file, networking or streams. It makes your test useless because you're not testing your code but the thing on the other side of the I/O pipe.
You can accumulate results in a buffer and then output them when the test is over. For example:
std::array<std::chrono::duration<double>, SOME_SIZE> results; for (auto& result : results) { result = std::chrono::system_clock::now() - startTime; } for (auto result : results) std::cout << result.count() << "\n"; // Use "\n" not to flush every line separately std::cout << std::flush; // Flush everything at once
-
To address the 2ton white elephant in the room:
There is no such thing as a real-time simulation on any desktop PC!
You are using a multi-tasking, preemptive (timesharing) system where background tasks can/will skew your results.
Correct simulations emulate real-time by keeping an internal pseudo-timer counter such that the "state" of the simulation can be restored to any point by running the sim until the pseudo-timer reaches the desired value.
You can sync your pseudo-timer to the RTC on the PC, but you need to understand that you cannot rely upon the RTC to timekeep for any real simulation.