04-13-2021 07:31 PM
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.
04-13-2021 08:16 PM
@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.
04-13-2021 08:23 PM
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.?
04-13-2021 08:41 PM
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
04-13-2021 09:22 PM
Search the forums.
Also, LabVIEW provides a template when you go New.... From Templates
04-14-2021
06:51 AM
- last edited on
05-14-2025
09:49 PM
by
Content Cleaner
@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?
04-14-2021 12:45 PM
Thanks for the tips!
-KDan
04-14-2021 01:15 PM
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.