CPU usage

Started by
12 comments, last by Storyyeller 14 years, 2 months ago
I have a simple question. When I run this: for(int i=0; i<2000000000; i++){ ; } Why does the CPU usage of the process in my task manager max out at 13% instead of using more of the idle CPU power? I have an i7 Windows 7 64-bit.
Advertisement
You aren't doing much in the loop, so all the code has to do is update the loop counter which hardly uses any cpu time at all.

Try this : Blink. Now realize that most modern computers could do a few billion calculations in the time it took you to blink.

Once you start adding in more complex things to do, CPU time will be used up a bit more. Once you add in multithreaded code then more of your CPUs will likely be used as well.
i7 has 4 cores + hyperthreading, which is reported as 8 cores in task manager.

Your loop is single-threaded, running on single core. 100%/8 = 12.5%
Ok so since I have a quad core, I can understand it just sticking this in 1 core and ignoring the other 3. What I don't understand is why it is 13% as opposed to 25%. If the thread fully utilizes the core it gets put on shouldn't it be able to complete the loop twice as fast? Looping through 2 billion numbers is not something that gets done in the blink of an eye.
Oh nm Antheus answered my question. Now understanding why hyperthreading actually helps is a different question...
Quote:Original post by jamesd128
Now understanding why hyperthreading actually helps is a different question...


It doesn't really help all that much for this type of things, but kernel reports 8 cores, and task manager uses that in calculations.

Quote:Looping through 2 billion numbers is not something that gets done in the blink of an eye.

If you were to compile that code with optimizations on, it should be a blink of an eye, since compiler should figure out the loop doesn't really do anything, and wouldn't run it at all.
A naive brute force solver I wrote once was still capable of trying somewhere around 28 million possibilities per second. Each test required looping a certain calculation 32 times, for a total of 900 million steps per second.
Each step involved several multiplications and additions.

Also, you're computer sounds like it's newer and faster then mine.

So yeah, it is reasonable to expect it to loop through 2 billion numbers in the blink of an eye, even with no optimizations.
I trust exceptions about as far as I can throw them.
Ok now I'm curious. I opened up visual studio 2008 and put this in an empty project:

#include<iostream>
#include<ctime>
int main(){

std::clock_t start=std::clock();

for(int i=0; i<2000000000; i++){
;
}

std::clock_t end=std::clock();
std::cout<<(end-start)/(double)CLOCKS_PER_SEC<<std::endl;
return 0;
}


When I run it, it takes 4 seconds, perhaps I'm just being too literal but that is not the blink of an eye.

So first off Antheus, why doesn't the compiler realize nothing is happening in the for loop and skip it? Is there some setting in Visual Studio to tell it to compile with optimizations which is not on by default?

Storyyeller I have a core i7 960 with a little overclock action so ya I got a fast computer. You said you could execute 900 million steps per second with several operations per step, assuming several = 3, that's 2.7 billion instructions executing 5.4 times faster than I get. Were you exaggerating with those numbers or am I missing something? I don't know much about performance coding so I'm wondering if there's some super special way to compile/run code that makes it run faster.
Build it in release mode instead of debug.
You should also check out the assembly output for a clearer picture of what is happening ( Compilers usually unfold simple loops ).

This topic is closed to new replies.

Advertisement