need code to peg CPU for 10 seconds

  • Thread starter Thread starter not_a_commie
  • Start date Start date
N

not_a_commie

I need code to peg the CPU for 10 seconds to test some background
threads. Sleep obviously doesn't do that. How do I code a busy loop
that won't get removed by the compiler? Thanks.
 
not_a_commie said:
I need code to peg the CPU for 10 seconds to test some background
threads. Sleep obviously doesn't do that. How do I code a busy loop
that won't get removed by the compiler? Thanks.

DateTime endTime = DateTime.Now.AddSeconds(10);
int i = 0;
while (DateTime.Now < endTime) { i++; }


You'd have to run 2 or 3 instances of this program at once to really peg the
CPU though...on my machine, something is preventing the app from using more
than 50% of the CPU per process...which, is pretty good since I don't want
any apps bogging down my system :)

HTH,
Mythran
 
not_a_commie said:
I need code to peg the CPU for 10 seconds to test some background
threads. Sleep obviously doesn't do that. How do I code a busy loop
that won't get removed by the compiler? Thanks.

Note on my prev thread, I have a hyperthreaded CPU which is why it is not
using more than 50% CPU on my machine...may end up using more for you
depending on your CPU..

HTH,

Mythran
 
not_a_commie wrote in message
I need code to peg the CPU for 10 seconds to test some background
threads. Sleep obviously doesn't do that. How do I code a busy loop
that won't get removed by the compiler? Thanks.
"Fields that are declared volatile are not subject to
compiler optimizations that assume access by a single thread."
(http://msdn2.microsoft.com/en-us/library/x13ttww7.aspx)

Regards,
Christian
 
Mythran said:
DateTime endTime = DateTime.Now.AddSeconds(10);
int i = 0;
while (DateTime.Now < endTime) { i++; }


You'd have to run 2 or 3 instances of this program at once to really peg the CPU
though...on my machine, something is preventing the app from using more than 50% of the
CPU per process...which, is pretty good since I don't want any apps bogging down my system
:)

This means you are running on an HT CPU, which means that this is pretty bad because your
CPU cannot server any other thread, or otherwise stated your system is bogged down by this
single thread.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top