Sign in to follow this  
jamesd128

CPU usage

Recommended Posts

jamesd128    122
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.

Share this post


Link to post
Share on other sites
vaneger    100
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.

Share this post


Link to post
Share on other sites
Antheus    2409
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%

Share this post


Link to post
Share on other sites
jamesd128    122
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.

Share this post


Link to post
Share on other sites
Antheus    2409
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.

Share this post


Link to post
Share on other sites
Storyyeller    215
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.

Share this post


Link to post
Share on other sites
jamesd128    122
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.

Share this post


Link to post
Share on other sites
Rattenhirn    3114
Quote:
Original post by jamesd128
Oh nm Antheus answered my question. Now understanding why hyperthreading actually helps is a different question...


Note the "hype" part in hyperthreading... ;)

Share this post


Link to post
Share on other sites
jamesd128    122
hah sweet, compiled in release mode and it took 0 seconds to complete

I'm using this now so the for loop will have some content:

int j=0;
for(int i=0; i<2000000000; i++){
j++;
}
std::cout<<j<<std::endl;

I can't tell from looking at the assembly if it's actually running 2 billion iterations or just assigning 2000000000 to j since that's what it does anyway. Is there an Assembly debugger type thing in Visual Studio which lets me step through the assembly while displaying the registers/memory?

Share this post


Link to post
Share on other sites
Antheus    2409
Quote:
Original post by jamesd128

I can't tell from looking at the assembly if it's actually running 2 billion iterations or just assigning 2000000000 to j since that's what it does anyway.


MSVC is capable of evaluating value of j during compile-time, resulting in same 0 running time, but it depends on values and data types.

Quote:
Is there an Assembly debugger type thing in Visual Studio which lets me step through the assembly while displaying the registers/memory?


Put a breakpoint on the statement where you wish to stop. Hit alt-8 to show assembly. If program just skips the breakpoint, then that statement, or anything related to it didn't get compiled. Release build may not break correctly either, or match source correctly, but that is a side-effect of optimizations.

Share this post


Link to post
Share on other sites
Storyyeller    215
Quote:

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.


Actually now that I think about it, I think I had it do calculations mod something first with the assistance of a lookup table and only do the full calculations if it matched. So it was probably really doing a lot less then 2 billion per second.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this