Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::cout is decreasing CPU Usage?

Tags:

c++

I'm running a program written in C++ where CPU Usage is an important factor, and have been working to decrease it until I came across an unexpected anomaly. The program uses a std::cout line to view performance, and when I remove it the CPU Usage of the program spikes dramatically in the Windows Resource Monitor for about 1 second at 4-6 second intervals.

With the std::cout line in place, it runs smoothly at 0-2% CPU Usage without any spiking. Removing the std::cout line, it runs at 0-2% CPU Usage, with the 4-6 second interval CPU Usage spiking as high as 25% within the Windows Resource Monitor.

Interestingly enough, increasing the size of the string being outputted reduces the height of the CPU Spike, and reducing the size of the string increases the height of the CPU Spike.

Here are the lines of code in question:

float engineCompletionTime = engineTimer->getEngineCompletionTime();

/* Stops CPU from spiking */
std::cout << "Engine cycle took " << engineCompletionTime << " milliseconds." << std::endl;

if (engineCompletionTime < 14.0f) {
    std::this_thread::sleep_for(std::chrono::milliseconds((int)(14.0f - engineCompletionTime)));
}

Edit: Thank you for your initial responses, but just to be clear:

When the std::cout line is in the program, there is no CPU Spike.

When the std::cout line is not in the program, there is a CPU Spike.

As the size of the string being printed by std::cout increases, the size of CPU spike decreases.

like image 906
Dark Soul Avatar asked Oct 27 '25 04:10

Dark Soul


1 Answers

Depending on the implementation, writing to std::cout basically involves handing control over to the operating system for a short while, which then sends the data to the terminal. This isn't the fastest process on windows, and during that time the program is basically idle. The more data std::cout has to process, the longer the delay takes, so the more time is spent idle by the program.

A program has low CPU usage if it spends a lot of time waiting around for input (either from another program, from the keyboard or mouse, from a "redraw the screen" signal if it has a GUI, from the operating system, or from the file system). Basically, the CPU usage represents the portion of the time that the program isn't waiting around.

If the program's only job is to process a lot of incoming data, with no delays, then it shouldn't be waiting around at all, and it's fine if there's a high CPU usage. On the other hand, if you have a program that is waiting around for user input (like an application that's interactive), having a high CPU usage can be problematic because it means the program is doing a lot of work, even when the user isn't doing anything.

Making the program more efficient will generally fix this problem.

Other reasons for high CPU usage

If you're checking for something to happen in a tight loop, instead of using event handling, that will cause high CPU usage because even though the program's waiting for something, it's actively checking for it. It's the difference between checking the door for a package every three minutes, and just waiting until the doorbell rights. If you check every three minutes, you'll be doing a lot of work even though you're just waiting. But if you just wait until the doorbell rings (as with event handling), you're not actually doing any work.

like image 92
Alecto Irene Perez Avatar answered Oct 28 '25 18:10

Alecto Irene Perez



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!