Code Comments
Programming Forum and web based access to our favorite programming groups.I have a simple while loop. It doesn't call vi's or do anything except loop. In fact, it is completely empty except for a boolean control attached to th e conditional terminal. As soon as I run the vi, my cpu usage goes up to 50%. Is this normal. I know I could put a delay in the loop and reduce the usage. When I do this if I p ut a 1ms delay in my usage stays close to zero. When I put in a 0ms delay, m y usage is again about 50%. I found this out because apparently, while looping and doing nothing, it is accessing the disk, and the buzzing was driving me nuts. (Not with this whil e loop, but with a real program that also has a while loop). So I was driven to investigate. Is this a labview thing, or does this happen with other programming language s too? I'm using lv6.1. Thanks. Chuck Gantz
Post Follow-up to this messageIt is normal that a loop without any wait statement will consume all availab le CPU. It will spin as fast as possible. Why shouldn't it? :o A while loop itself does not consume huge amouts of memory, so your other ap plication certainly has a different issue. Can you post the code? Are you bu ilding arrays to infinite size? What else is in that other loop?
Post Follow-up to this messageI'll have to try a simple experiment when I get home. I'll write a console a pp in C with just a while loop and see if the same thing happens. I can't really send the other app, but it is essentially a while loop that w aits for me to press a button before doing measurements et al. Normally it just sits and waits. The problem is that if I try to run somethi ng else (specifically Excel) while the program is waiting, the pc is so sluggish that I can't do anything. As soon as I turn off my LV program, the pc is fine. Chuck Gantz
Post Follow-up to this messageLike Altenbach said, when you have no wait, the loop tries to run as fast as possible, regardless of what you have in it and regardless of what other pr ograms want. This is probably specific to the LV compiler. I'm not sure how parallel programming works in other languages. Can you write a loop which will be truly stand-alone (fr om the compiler's point of view)? If you can, how do you control its timing? Using wait statements? If you put a 0 ms wait, LV should try to run the loop as fast as it can as l ong as no other tasks are requesting the CPU, which is probably why you're s eeing a high CPU usage there.
Post Follow-up to this messageThanks all.
I went home and wrote my simple C program with a while statement:
int _tmain(int argc, _TCHAR* argv[]){ while( true) {} return
0;}
When I ran it my cpu usage went up to 50%, just like with LV. Opened my eyes
. I thought the OS would somehow regulate this. Now I know better.
I also now know why I see the Wait for nearest ms vi in so many examples. I'
ll just start to include it myself.
Chuck Gantz
Post Follow-up to this messagecgantz2000 wrote: I have a simple while loop. It doesn't call vi's or do anything except loop. In fact, it is completely empty except for a boolean control attached to th e conditional terminal. As soon as I run the vi, my cpu usage goes up to 50%. Is this normal. I know I could put a delay in the loop and reduce the usage. When I do this if I p ut a 1ms delay in my usage stays close to zero. When I put in a 0ms delay, m y usage is again about 50%. As an addition note, it would be more typical that the loop consumes 100% of the CPU. Do you have a dual-core processor? You have three scenarios (approximate times for my PC, normal priority, debu gging enabled): - No wait: The loop runs as fast as the CPU allows.Only <a href="h ttp://ideasinwiring.blogspot.com/2005/05/55ms.html" target="_blank"> every 5 5ms</a>, it checks if something else needs attention, then continues. The lo op spins about 100 million times/second. Actual rate depends on CPU speed. - 0ms wait: The loop runs as fast as the CPU allows. After every iteration, it checks if something else needs attention, then continues. This slows down the loop rate to about 3 million iterations/second. Actual rate depends on CPU speed. - 1ms wait: Same as case (2), but the loop waits ~1ms before continuing. Sin ce we just saw that one loop iteration takes only a few nanoseconds, the CPU is nearly 100% idle. The loop spins exactly 1000 iterations/second, irrespe ctive of CPU speed. Cases (1) and (2) both should consume 100% CPU. There is always somethi ng to do, no time for idling. Right?
Post Follow-up to this messageOn my main machine at work running LV with just a while loop and no del ays, and my pc at home running the C while loop, 50% of the resources were u sed. On the machine at work running the "real" LV program, the one that actu ally does something, 100% o f the resources were used. No dual processors. I don't know the last machine used twice as many resource as the other two, but it is an older machine and may have other issues. I don't really know wh at was causing the noise on my desktop pc when the while is running. I assum ed it was disk access simpl y because there is not much else in the pc that causes noise, but it could b e something else. Chuck Gantz
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.