Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 36 ยท Deployment

Performance and Memory Optimization

Make LabVIEW applications fast and lean: use the Profile Performance and Memory tool, avoid array copies, preallocate, use in-place structures, parallel loops and efficient data logging.

Most LabVIEW programs are fast enough until the day they are not: a 24-hour acquisition fills the memory, a loop takes 5 seconds instead of 50 milliseconds, a graph freezes. Before you tune anything, measure. This lesson shows what LabVIEW gives you for measuring and the most common wins, and uses Python stand-ins to demonstrate the same ideas with real timings you can run.

LabVIEW is not available on this site. Its tools are described step by step, and no LabVIEW timing is quoted; the Python timings are only a model of the principles.

Measure first

  • Tools > Profile > Performance and Memory... opens the profiler. Click Start, run your application, click Snapshot. A table lists each VI with run counts, time (total, average, min, max), and memory (blocks, bytes). Sort by VI Time to find the hot spots; tick Timing statistics and Memory usage to collect more detail.
  • Tools > Profile > Show Buffer Allocations... marks places on the diagram where LabVIEW allocates a buffer (a black dot on a wire or terminal). Each dot may be a data copy.
  • Tick Count (ms) and High Resolution Relative Seconds around a section measure a small piece exactly. Subtract the two readings.
  • The Desktop Execution Trace Toolkit (add-on) records events of a running application for deeper analysis.

Rule one: do not optimise what the profiler does not flag.

Arrays: avoid copying

LabVIEW tries to reuse memory, but some operations force copies. Typical culprits:

  • Build Array inside a loop: each iteration reallocates a larger array, which is O(n squared) overall. Instead preallocate with Initialize Array and use Replace Array Subset in the loop, or use a For Loop's auto-indexing output tunnel, which allocates efficiently.
  • Auto-indexing outputs on a While Loop grow the array the same way; fine for small sizes, costly for millions.
  • Reading a large array into several branches that modify it: LabVIEW has to copy on the branch that writes. Use the In Place Element Structure (and Data Value Reference for shared large data) to modify an element in place without a copy.
  • Showing 1M points on a graph each iteration: decimate before display and update less often.
Python Why preallocating beats appending (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Strings and files

  • Concatenating strings in a loop (Concatenate Strings) is like Build Array. Collect pieces in an array and use Concatenate Strings once, or write to a file in chunks.
  • Open a file once before the loop, write in the loop, close once after. Opening and closing per iteration is slow.
  • Prefer binary files (TDMS or raw) for high-rate data; ASCII spreadsheets are slow and large. TDMS is NI's binary format for measurement data with built-in metadata and good write speed.
  • Use the Write to Measurement File Express VI only for light use; for streaming use TDMS Write with a producer/consumer (producer/consumer) so disk speed does not stall acquisition.

Loops and the user interface

  • Wait (ms) in loops that do not need to spin; a loop with no timing uses a whole CPU core.
  • Updating indicators is relatively expensive; update every 50 to 100 ms rather than every iteration, or defer front-panel updates (VI Properties > Execution, or the Defer Panel Updates property) while changing many controls.
  • Property nodes are slow compared with wires because they run in the UI thread; avoid them in tight loops. Prefer wiring to indicators.
  • Local and global variables copy data on every read. Use a functional global, a data value reference or a queue for large shared data.

Parallelism

LabVIEW runs independent code on multiple threads automatically. To benefit:

  • Enable parallel For Loop iterations: right-click the loop border, choose Configure Iteration Parallelism, and set the number of generated parallel instances (often the number of logical processors). Iterations must not depend on each other.
  • Keep parallel branches independent: do not share the same subVI instance unless it is Reentrant (VI Properties > Execution > Preallocate clones for each instance or Share clones between instances). A non-reentrant subVI can only run in one place at a time, so parallel callers wait for each other.
  • Avoid doing UI work in the loop you want to speed up.
Python Estimating a parallel speedup

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Memory: long-running programs

  • Queues and arrays that grow are the main cause of "the program slowly eats all the memory". Cap their size and watch the backlog.
  • Close or release references: queues, files, VISA sessions, DAQmx tasks, VI references, notifiers and user events. A leak per iteration adds up. After a clean exit, nothing should remain.
  • Large datatypes in shift registers are fine; copies of them in several branches are not.
  • Use Request Deallocation (Memory Control) only for rare, very large temporary buffers; routine use usually makes things slower.
  • In Task Manager, watch the memory of your app for hours; it should level off.

Choosing data types

Use the smallest numeric representation that is correct: I32 or SGL where a DBL is not needed, and a waveform only where the time base is useful. Convert to a plain 1D array for heavy maths. Booleans in arrays are bytes; for huge flag sets pack into integers. Avoid variants in tight loops: converting to and from them costs time.

A tuning routine

  1. Define a target ("a full cycle in under 100 ms").
  2. Profile with real data volumes.
  3. Fix the top item (copies, I/O, UI updates).
  4. Re-measure. Stop when the target is met.
  5. Keep the profile numbers in your notes for the tests that guard against regressions.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is Build Array inside a loop slow for large arrays?

    Repeated growth copies data; preallocate or use auto-indexing instead.
  2. 2. Which tool lists time and memory per VI?

    The profiler (Tools > Profile) reports run counts, times and memory use per VI.
  3. 3. A parallelised For Loop runs no faster. What is a likely cause?

    Dependencies and non-reentrant subVIs prevent real parallel execution.
  4. 4. What should you do about a file you write every iteration?

    Opening and closing is slow; open once, write many times, then close.

Summary

  • Profile first: Performance and Memory tool, buffer allocations, tick counts.
  • Preallocate arrays, avoid copies, use in-place structures and efficient file formats.
  • Limit UI work, avoid property nodes in loops, and use reentrant subVIs for parallel code.
  • Cap queues, release references and watch long-run memory.

Next: Project: A Multi-Channel Data Logger