100% CPU

  • Thread starter Thread starter bill salkin
  • Start date Start date
B

bill salkin

The simple VB.NET loop:

for i = 1 to 30000000
' do something
next

drives the CPU on my computer to 100% utilization for the
duration of the loop. (Several minutes)

How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?


TIA,

Bill
 
The simple VB.NET loop:

for i = 1 to 30000000
' do something
next

drives the CPU on my computer to 100% utilization for the
duration of the loop. (Several minutes)

How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?


TIA,

Bill

Add this:

System.Threading.Thread.Sleep(0)

Specifying 0 for the sleep will cause the thread to suspend so that
other waiting threads can execute.

// CHRIS
 
The simple VB.NET loop:

for i = 1 to 30000000
' do something
next

drives the CPU on my computer to 100% utilization for the
duration of the loop. (Several minutes)

How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?


TIA,

Bill

Thought I should add that if you're doing a loop that many times,
30 million, you should also limit the number of times you call Sleep.
The overhead involved will definitely add up.

Maybe something like this:

for i = 1 to 30000000
if i mod 5000 = 0 Then System.Threading.Thread.Sleep(0)
next i


// CHRIS
 
Throw an Application.DoEvents() call into the body of the loop.

Beware of using a DoEvents(). Calling this method can cause code to
be re-entered if a message raises an event.

// CHRIS
 
Hi Bill,

I never tried it, but did you tried the
threading.thread.priority = threadpriority.lowest

Will you give us a sign if it did help than we know it also?

Cor
 
bill salkin said:
How can I modify this loop to let other (unrelated)
applications running on the same box get some CPU time?

That's the entire point of multi-threading -- the operating system will do
this for you. Simply keep the code you're currently using. Whenever another
application also needs CPU time, the operating system will split the
available time over the two processes.
If your application is running at 100% CPU time, it means that there's no
other application that requires processor time.

If you wish to run your code in a low-priority background thread, take a
look at the Thread.CurrentThread.Priority property.

Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl
 
Hi,

If you place it in a thread the cpu usage will stay at 100%.
However the program will not lock up. You still can start other procedures
and handle events. The thread will only run when it has time. Here is an
example. I assume you have a form level variable mbStop and a cancel button
btnCancel.

Public Sub MyLongProcess()

For i As Long = 1 To 30000000

Me.Text = i.ToString

If mbStop Then Exit For

Next

MessageBox.Show("Done")

End Sub

Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnCancel.Click

mbStop = True

End Sub

To start thread

Dim trdMyLongProcess As New Threading.Thread(AddressOf Me.MyLongProcess)

mbStop = False

trdMyLongProcess.Start()

Ken
 
* Chris Morse said:
Beware of using a DoEvents(). Calling this method can cause code to
be re-entered if a message raises an event.

ACK. And it won't reduce CPU usage...

:-)
 
That's the entire point of multi-threading -- the operating system will do
this for you. Simply keep the code you're currently using. Whenever another
application also needs CPU time, the operating system will split the
available time over the two processes.

Yes, in theory, but in practice this is sometimes not the case. I
have had "runaway processes" (like, once in a while, the File Transfer
window in pcAnywhere) that hog the CPU almost completely and make it
almost impossible to do anything else.
If your application is running at 100% CPU time, it means that there's no
other application that requires processor time.

Absolutely not. Dozens of processes are then competing for what
little CPU time that's left over. You should avoid 100% cpu
utilization if possible. Adding a sleep() call now and then can
drastically improve overall system performance. Likewise, as Cor
pointed out, you can run the thread at a lower priority. This will
help keep a CPU hog from (what appears to be) locking the system up;
though you'll still have 100% CPU utilization; just that other
processes will get more time to do their thing.
If you wish to run your code in a low-priority background thread, take a
look at the Thread.CurrentThread.Priority property.

That's a good idea.
Regards,
Pieter Philippaerts
Managed SSL/TLS: http://www.mentalis.org/go.php?sl

// CHRIS
 
Hi Chris,

Application.DoEvents DOES NOT issue a call to Sleep. There are reasons to
use DoEvents, but this is not one of them. You include a call to DoEvents
IF you application itself, in the same thread, need to process events.
However, even then it is best to call Sleep as the other message suggested,
in addition to calling DoEvents.

If the thread in question does not need to process events while the loop is
executing, then Sleep is all that is needed, and all that (often) should be
used.

Dick

--
Richard Grier (Microsoft Visual Basic MVP)

See www.hardandsoftware.net for contact information.

Author of Visual Basic Programmer's Guide to Serial Communications, 3rd
Edition ISBN 1-890422-27-4 (391 pages) published February 2002.
 
Cor,

Here are my results so far:


1. The statement

If i Mod 2 = 5000 Then
System.Threading.Thread.Sleep(0)
End if

has almost no impact on CPU utilization (as viewed through
Win 2K Task Manager). The same seems to be true for setting

2.
Threading.Thread.CurrentThread.Priority =
Threading.ThreadPriority.Lowest

I find in both cases that the app still hogs around 97%
of the CPU but suprisingly the loop completion time
increases only slightly in either case (a fraction of a
second for 300,000 iterations)

Bill
 
On Mon, 29 Dec 2003 13:32:00 -0700, "Dick Grier"

(SNIP)
Application.DoEvents DOES NOT issue a call to Sleep. (SNIP)
Dick

Sorry- Apparently my bad.

My statement is TRUE for VB 4.0/5.0/6.0, though. That is, if you
believe the MSDN documentation (see "HOWTO: Determine the Differences
Between DoEvents and Sleep", Q158175.)

The .NET documentation does not say anything about it either way (that
I can find.)

// CHRIS
 
I find in both cases that the app still hogs around 97%
of the CPU but suprisingly the loop completion time
increases only slightly in either case (a fraction of a
second for 300,000 iterations)

Bill

Yes, I thought you were trying to avoid the "lock up" problem, where
other windows stop responding (or do so VERY slowly.)

If you want to decrease your CPU utilization, you'll have to put in
positive sleep values (try different values for the MOD value and the
Sleep(n) value).

The more you sleep, the less the CPU usage, and the longer it will
take for the loop to complete.

For k = 1 To 30000000
If k Mod 5000 = 0 Then
System.Threading.Thread.Sleep(1)
End If
Next k

The value of 5000 is arbitrary. The more "work" you're doing in your
loop, the lower the value you'll want to use. Otherwise, it will make
little or no difference at all.

Play around with the numbers and get a feel for what combinations do.
Doing a Sleep(1) means (ideally) a 1 millisecond sleep. So that means
you can only do that 1000 times a second.

Since the k Mod 5000 allows it to go every 5000 itterations, your loop
will have an overhead the following overhead:

30 million itterations
times
1 millisecond sleep / 5,000 itterations

= 6,000 milliseconds sleep

= 6 seconds

You have to ask yourself how long you're willing to let the loop take
-vs- how much CPU usage you want to let it use.

// CHRIS
 
Hi Bill,

That the application uses the CPU normaly at about 97% is in my opinion
normal, I have that with every application.

However now you are testing, why do you not write two test programs, one
with the thread.sleep and one with the thread.priority lowest.

Start them at the same time and see the result.

I am curious for your answer.

Cor
 
One problem, the load will be different depending on CPU type/speed.

How do you work arround that?
Maybe check spu load?

Schneider
 
Back
Top