Hi all,

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%.

Can I use Sleep API to control CPU. Is it good programming practice?

Is there any effective solution to control CPU usage?

Thanks in advance.
--
Thanks & Regards,
Alex.

Re: How to control cpu usage by nathan

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

Re: How to control cpu usage by Klueless

Klueless
Wed Jan 09 12:20:31 CST 2008

"Alex" <Alex@discussions.microsoft.com> wrote in message news:E33C4B74-59CF-4D30-BBCD-9D32882AAECC@microsoft.com...
> 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%.
> Can I use Sleep API to control CPU. Is it good programming practice?
> Is there any effective solution to control CPU usage?

Perhaps lowering your own process' "priority" is a better solution.
If no other program really wants the CPU anyway, your program could
still take full advantage of it. Try

SetPriorityClass BELOW_NORMAL_PRIORITY_CLASS




Re: How to control cpu usage by Brian

Brian
Wed Jan 09 12:29:58 CST 2008


"Alex" <Alex@discussions.microsoft.com> wrote in message news:E33C4B74-59CF-4D30-BBCD-9D32882AAECC@microsoft.com...
> Hi all,
>
> 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%.
>
> Can I use Sleep API to control CPU. Is it good programming practice?
>
> Is there any effective solution to control CPU usage?
>

The most effective solution is to write efficient code. This typically entails profiling your application to understand where the
cpu-intensive code lies, and then focus on optimizing that code.

Apart from that, why do you care if the cpu reaches 90-95%? Are other vital applications being starved?

Brian




Re: How to control cpu usage by Alex

Alex
Wed Jan 09 22:04:00 CST 2008

Thank you all (Nathan Mates, Klueless and Brian).

Hi Brian,

Our application is related to files & folders. If user selects
"MyComputer" as an input, then it has to enumerate whole computer and it
touches all files and folders in pc. It takes hours. While running this
application, User cannot go for other applications. Because it takes 90- 95%
cpu.

I will try with this:SetPriorityClass THREAD_PRIORITY_IDLE or
BELOW_NOTMAL.

If you any idea, share with me.

Thanks in advance.
--
Thanks & Regards,
Alex.


"Brian Muth" wrote:

>
> "Alex" <Alex@discussions.microsoft.com> wrote in message news:E33C4B74-59CF-4D30-BBCD-9D32882AAECC@microsoft.com...
> > Hi all,
> >
> > 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%.
> >
> > Can I use Sleep API to control CPU. Is it good programming practice?
> >
> > Is there any effective solution to control CPU usage?
> >
>
> The most effective solution is to write efficient code. This typically entails profiling your application to understand where the
> cpu-intensive code lies, and then focus on optimizing that code.
>
> Apart from that, why do you care if the cpu reaches 90-95%? Are other vital applications being starved?
>
> Brian
>
>
>
>