Question

I have a small program which attempts to find the largest prime factor of a very large number. I have written this program in C#. It is brute force and will take quite sometime to complete. This is deliberate as I'm trying to understand how the CPU is utilised.

How can I get it to fully utilise the CPU. At the moment it only ever uses about 25% while for about 68% of the time the CPU is just idle: enter image description here

Why does the program not use all the available CPU cycles?

Update:

The CPU history for the four cores looks like thisenter image description here

Why are none of these right up at the top?

Was it helpful?

Solution

If your computer has a multicore architecture (which is likely), the only way you'll get a high amount of usage is if you make use of multiple threads.

For example, if your computer is a quad-core, you'll only use 25% of the CPU if you use a single thread.

By using multiple threads (either via explicit multithreading code or via something like Parallel.For) you'll get something close to 100%, but not exactly - there are always overheads that keep programs from achieving full usage. The nature of the algorithm (i.e. how parallelisable it is) also makes a big difference. Something like ray tracing is embarrassingly parallel and the overheads are miminal.

OTHER TIPS

I'm guessing you have 4 CPUs/cores, and since you're running a single threaded process, you'll use only one of those CPUs, hence your 25% usage figure. If you can parallelise this (dependent upon your algorithm) then you can invoke (say) 4 threads and that should distribute across your CPUs (it's dependant upon your OS scheduler, so this is something you can't guarantee completely)

Can it be that you have four CPU cores? In case the programm runs on one/fourth of your system with 100 % it could show up as 25 % in the process list.. what does the multi threat performance bars say?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top