By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,490 Members | 1,417 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,490 IT Pros & Developers. It's quick & easy.

need code to peg CPU for 10 seconds

P: n/a
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.

Feb 5 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a


"not_a_commie" <no********@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
>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
Feb 5 '07 #2

P: n/a


"not_a_commie" <no********@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
>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
Feb 5 '07 #3

P: n/a
not_a_commie wrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
>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
Feb 5 '07 #4

P: n/a
"Mythran" <ki********@hotmail.comwrote in message
news:AA**********************************@microsof t.com...
>

"not_a_commie" <no********@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
>>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
:)
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.

Feb 5 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.