LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

CPU usage

Hi,

I am trying to reduce CPU usage, it is about 50, how can I do this? My CVI environment sleep policy (under option, environment...) is set to sleep more.

 

In my program:

 

- There is a timer function that helps the program to start automatically when lunch. In the timer function there is a call to another function that is the heart of the program, it has a "do..while" loop that is contineously running until a stop signal is sent. Inside that loop the test measurment is done every 4 sec.

 

also "ProcessSystemEvents ();" is used inside the do..while loop.

 

 

Outside this function there is another function that implement a datasocket...

 

What could be making this program heavy and how could I solve this.

 

 

Thank you.

0 Kudos
Message 1 of 3
(3,020 Views)

You have what is called a "spinlock" or "busy loop" and that is a big no-no when it comes to efficiency.  It's actually considered rude if you have your application doing that, since it can block other applications from running efficiently.

 

There are a multitude of ways to improve program efficiency.

 

A simple way would be to put a SleepEx()  call in your busy loop (I'm assuming you're running on a Windows OS) so that your process's main thread gives up the CPU periodically, reducing the time it has the CPU and allowing other threads to run.  In your case, simply have the do while loop include a sleep for 4 seconds i.e. Sleep(4000); Since your measurement can only update every 4 seconds, you only need to check for data every four seconds.

 

Note that if you do a Sleep() call on your main thread (the thread that loaded your GUI interface), the GUI will become unresponsive to user input while the thread is suspended.  You can construct a wait loop with a Sleep and a ProcessSystemEvents call to get the advantage of sleeping while still keeping the user interface responsive.

 

So, in the simplest form, you could do maybe 8 half-second Sleep() calls    Sleep(500) with a Process system events innetween each:

 

Sleep(500);

ProcessSystemEvents();

Sleep(500);

ProcessSystemEvents();

...

 

of course you should make a loop out of this and maybe decrease the sleep time to 250 milliseconds.

 

If you use the NI Delay() function, it may not sleep as much if at all, there have been reports of the Delay() function spinlocking.

 

You can use multiple threads to increase the efficiency of a process - the idea being that be delegating tasks to individual threads, they are able to run efficiently by sleeping when they're not needed.  This isn't as easy as it sounds, but professionally developed Windows applications are almost exclusively multi-threaded.

 

Another way to increase efficiency would be to use an asynchronous callback that is scheduled whenever you have a incoming data on the datasocket, that way your main thread can stay idle (simply run the user interface).

Message 2 of 3
(3,018 Views)

Hi Menchar,

 

Your perfectly got this. It seems to improve it a lot. Thanks for your quick, prompt and perfect solution.

 

I never tried that asynchronous call for the datasocket but I will try. You may expect another question here. But let me try.

 

Anotherwise, you just gave me a good solution already.

 

Thank you.

0 Kudos
Message 3 of 3
(3,011 Views)