LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

While loop slows down

Hello- I'm a Biology graduate student that has a very superficial knowledge of LabView.

 

I wrote a VI (attached) to run mouse behavior experiments. The VI has 2 components- a while loop that's positioned at the top which control NI-DAQ outputs (solenoid valves mostly) and write NI-DAQ sensor inputs (optical encoder, pressure sensor, capacitative touch sensor), as well as a for-loop positioned below with sequences that run through "trials" during which the output variables are reassigned at different intervals.

I've noticed that if I run the program for over 20-30 minutes, the while loops take longer and longer to execute. I have no idea why but my guess is it has something to do with the fact that each while loop iteration adds onto a data array through a shift register.

 

Q: Is there a more efficient way to write this VI to ensure that I'm getting the fastest while loop iterations possible? ie How can I make this VI more efficient?

 

PS I apologize if the code is horrendous- I don't know what I'm doing.

0 Kudos
Message 1 of 8
(2,176 Views)

@dhlee47 wrote:

I've noticed that if I run the program for over 20-30 minutes, the while loops take longer and longer to execute. I have no idea why but my guess is it has something to do with the fact that each while loop iteration adds onto a data array through a shift register.


Yeah, that'll slow things down because you are hammering the memory manager requesting more and more memory.  So some things you can do instead:

1. Just wire the latest data to your charts.  Charts have history, so you will see that last X data and it will manage the circular buffer for you.

2. Log the data as you collect it.  To do this, you will need to learn to use the File/IO API.  Create the file before the loop, close the file after the loop, and write as much as you want inside of the loop.



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
0 Kudos
Message 2 of 8
(2,154 Views)

Why do you feel the need to collect all of the samples in an array in the loop?

 

It looks like you are just collecting them to write them to a file after the loop ends.  Why don't you use them in a producer/consumer pattern so that they get written to a file in a separate parallel loop.?

Message 3 of 8
(2,147 Views)

Thanks for your quick response-

 

The answer is that I don't know how to write a producer/consumer pattern. Do you have any suggestions on resources/examples that I can use to learn?

 

Thanks again!

-KDan

0 Kudos
Message 4 of 8
(2,130 Views)

Search the forums.

 

Also, LabVIEW provides a template when you go New....   From Templates

0 Kudos
Message 5 of 8
(2,124 Views)

@dhlee47 wrote:

The answer is that I don't know how to write a producer/consumer pattern. Do you have any suggestions on resources/examples that I can use to learn?


Producer/Consumer



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 6 of 8
(2,088 Views)

Thanks for the tips!

 

-KDan

0 Kudos
Message 7 of 8
(2,053 Views)

On a side note:

 

While doing the introductory LabVIEW tutorials, pay close attention to the rules of dataflow. For example you have (at least) three independent code sections that all will independently run as soon as the execution starts.

 

Execution order is fully determined by data dependencies. LabVIEW code does not execute left to right and I don't think you want to turn everything off (C), start the main loop (A), and wait 50ms (B) all at the same time.

 

altenbach_0-1618423873324.png

 

Message 8 of 8
(2,048 Views)