Tim Prince said:
Nirvana said:
Hello All,
I'm using for loop to manipulate a 2D int array of size [20000000][5].
The program is occupying 99% of CPU on my Win XP OS.
How to make this program to occupy less cpu ?
If your loop is small enough, depending on which platform you have in mind,
you may need to unroll the short loop entirely.
That might make the program finish more quickly, but it's not likely
to make it consume less than 99% of the CPU while it's running.
If you run a single CPU-intensive program, the operating system is
likely to allow that program to use most of the available CPU time.
Is that a problem? Was there something else you wanted to use the CPU
for while the program is running?
Your program is going to take a certain number of CPU cycles to run.
If you can convince your operating system to give only 50% of the CPU
to your program, it will take twice as long to finish. How to do that
is not a C question, and we can't help you with it here.
Reducing the number of cycles is a matter of optimization, something
we can't help you with without more details. Picking a better
algorithm and/or invoking the compiler with optimization options are
the most obvious things to try. Manual micro-optimization of your
source code, like unrolling your inner loop, is unlikely to be a good
use of your time; your compiler may do a better job of this than you
can, and your attempts may interfer with that.