LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Efficient use of flags in case structures

Solved!
Go to solution

Hi,

 

I am working with an RT target that handles several loops in parallel that are controlled from a PC via network-published share variables. I am handling timing between loops using boolean flags (single process variables) and I had a question about efficiency:

 

The following snippet shows the two cases, they are not parallel loops! Thought is was the easiest way to illustrate my question:

 

Best use of flags.png

 

The snippet above shows the two cases. Which of them are more efficient per cycle? Writing the flag each time, or reading the flag and only changing it if it is different? This is a question that always bugs me: is it cheaper in resources to read or to write variables?!

 

Also, would I gain a lot by switching to notifiers? Which one is a better practice for flagging between loops in an RT environment?

 

Thanks!

--
Smarter than the average bear!
0 Kudos
Message 1 of 6
(3,285 Views)

I DESPISE Network Published Shared Variables.  They have done nothing for me but give me headaches.

 

Instead, use TCP/IP or Network Streams to send data and messages to/from the RT.  You can have a single thread that just handles reading from the connection and send the message/data to whoever needs it via a queue, global variable, notifier, etc.  Have a look at the STM library for using the TCP/IP setup.

 

Now, to the Single Process Shared Variables...Do you only have 1 place you write to these variables?  If not, you will run into issues with race conditions in which case you might have to think about combining loops.

 

Assuming only 1 place is doing the writing, just write to the variable and be done.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 2 of 6
(3,243 Views)

@crossrulz wrote:

I DESPISE Network Published Shared Variables.  They have done nothing for me but give me headaches.

 

Instead, use TCP/IP or Network Streams to send data and messages to/from the RT.  You can have a single thread that just handles reading from the connection and send the message/data to whoever needs it via a queue, global variable, notifier, etc.  Have a look at the STM library for using the TCP/IP setup.

 


I'm using network streams for the important bits of communication (e.g. sensor data from RT to PC), and Network published variables for simple things (e.g. Start test command). Haven't had any issues besides the delay you get the first time you deploy the variables. Why do you despise them that much?

 


Now, to the Single Process Shared Variables...Do you only have 1 place you write to these variables?  If not, you will run into issues with race conditions in which case you might have to think about combining loops.

 

Assuming only 1 place is doing the writing, just write to the variable and be done.


I am only doing the writing of the flag in one place, so that is not an issue, but go back to the cases in the snippet: case A will write FALSE to the variable on EACH cycle and case B only writes FALSE if the variable is true (but needs to read the variable on each cycle). I just want to know which one is a better practice from a resource (CPU usage) point of view.

 

If I have time I will see if it's easy to benchmark using the RT CPU usage function.

 

And my other question: flags (using single proces shared variables) or notifiers on RT?

--
Smarter than the average bear!
0 Kudos
Message 3 of 6
(3,210 Views)
Solution
Accepted by topic author Vince84

Does it really matter? Is your code so tight on clock cycles that a difference of a few processor instructions will make a difference in your application performance? Even if we could give you a definitive answer for one version of LabVIEW, it might not apply for other versions of LabVIEW as NI makes improvements. Either of these approaches looks reasonable to me, so unless you're actually seeing a problem I wouldn't worry about it.

 

As for what to use on RT - do you need hard deterministic behavior? A notifier won't be free of jitter, but I would expect the amount of jitter to be negligible, particularly if you're just passing around a boolean. In general I like notifiers, and I've never used shared variables. On RT a single-process variable has the option of using an RT-FIFO to eliminate jitter, if that's an issue for you.

0 Kudos
Message 4 of 6
(3,182 Views)

It was me being curious, and just wanting to know if people already checked that. I actually benchmarked it today and saw no difference in CPU load doing it, so that's it xD

 

I don't need hard deterministic behavior, so I could switch my single-process variables with notifiers, just for that "good looking code" feel.

 

Thanks!

 

 

 

 

--
Smarter than the average bear!
0 Kudos
Message 5 of 6
(3,177 Views)

@crossrulz wrote:

I DESPISE Network Published Shared Variables.  They have done nothing for me but give me headaches.

 

Instead, use TCP/IP or Network Streams to send data and messages to/from the RT.  You can have a single thread that just handles reading from the connection and send the message/data to whoever needs it via a queue, global variable, notifier, etc.  Have a look at the STM library for using the TCP/IP setup.

 


It took me a good part of the week, but I replaced all my network published variables with a nice message system using a cluster/queue/network stream system, and wow, everything is tidier and easier to manage. I would kudo you again, but I can't!

--
Smarter than the average bear!
0 Kudos
Message 6 of 6
(3,127 Views)