nathan
Wed Jan 09 12:14:26 CST 2008
In article <E33C4B74-59CF-4D30-BBCD-9D32882AAECC@microsoft.com>,
=?Utf-8?B?QWxleA==?= <Alex@discussions.microsoft.com> wrote:
> I am developing an application with visual studio 6.0. It is full of
>while and for loops to process data. While running this application, CPU
>reaches 90 - 95%.
In my book, if an app has a legitimate need to chew up the CPU,
it's fine to do so. For example, when doing an intensive bit of
processing to a crazy large file in a graphics editor
(e.g. photoshop), or rendering a 3D scene, people want things to be
done as quickly as possible. In general, if the task was initiated
by the user, and the app's in the foreground, I'd say burn all the
CPU time you want.
This doesn't mean you should behave stupidly -- don't busywait for
another thread or asynchronous operation to finish. [See
http://en.wikipedia.org/wiki/Busy_waiting for info if you don't
recognize the terminology.] If you're a Win32 app, generally don't
busywait for another message to come in, unless you're a game that
needs to keep a framerate up.
> Can I use Sleep API to control CPU. Is it good programming practice?
You can, but it'll just make everything take longer. In my examples
of a graphics editor or 3D scene rendering above, I'd say that
sleeping is a bad idea. If you're not the foreground app, or otherwise
running in the background, then calling Sleep(1) periodically will
throttle your app's CPU usage. If nothing else is running, then
that'll just make the System Idle Process get a lot of CPU cycles,
which I consider a bit of a waste. People don't buy CPUs to make the
idle process finish faster.
It might be better to just call SetThreadPriority (see
http://msdn2.microsoft.com/en-us/library/ms686277(VS.85).aspx ) to set
your app THREAD_PRIORITY_IDLE or BELOW_NOTMAL. If you use that, any
other app that wants CPU time will get it instead of you, but you'll
tend to get more cycles than the idle process.
Nathan Mates
--
<*> Nathan Mates - personal webpage
http://www.visi.com/~nathan/
# Programmer at Pandemic Studios --
http://www.pandemicstudios.com/
# NOT speaking for Pandemic Studios. "Care not what the neighbors
# think. What are the facts, and to how many decimal places?" -R.A. Heinlein