Measure Time Passed on a Running VI

Updated Nov 10, 2020

Reported In

Software

  • LabVIEW

Issue Details

  • I am want to measure the time it takes my code to run. What VIs should I use?
  • What is the best way to measure how much time has passed on a VI that is running?
  • How can I benchmark my LabVIEW code?

Solution

To measure elapsed time in a VI that is running, the best way is to use a Flat Sequence Structure  in conjunction with a timing VI. Because a Flat Sequence Structure forces code to run sequentially, you can use a timing VI to measure time before and after your code runs. Subtract the time measurement before from the time measurement after to get elapsed time. 

The timing VI you should use depends on your use case:

  • For simple code measurements, use the Tick Count (ms) function. 
    • This is a good timing structure if your code runs on the order of milliseconds even up to days
    • The code being measured in the snippet below is a Wait (ms) function. To benchmark your code, replace the Wait (ms) function with your own code. 
    • The snippet below will run once and give you a single timing comparison. If you would like to run code multiple times to get an average runtime, you can use the Measure Code Execution Time in LabVIEW example code which iterates code and outputs the average execution time.


 Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram. 

  • If your code needs to run on the order of months, use the Get Date/Time in Seconds function for timing measurement.
    • Again, the code being measured in the snippet below is a Wait (ms) function. To benchmark your code, replace the Wait (ms) function with your own.

 Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram.
  • If you are benchmarking code on a Real-Time target, you can use the Tick Count Express VI and configure it to show the elapsed time in ticks so you will have a more precise result.
    • Note that you can use this function on a Windows or other non-deterministic OS, but it will only have precision up to the millisecond.
    • Again, the code being measured in the snippet below is a Wait (ms) function. To benchmark your code, replace the Wait (ms) function with your own.

 Note: This image is a LabVIEW snippet, which includes LabVIEW code that you can reuse in your project. To use a snippet, right-click the image, save it to your computer, and drag the file onto your LabVIEW diagram. 

Additional Information

  • The Tick Count (ms) VI returns a 32-bit number (0 to 4 billion), which means, when using the above method to measure elapsed time, the Tick Count (ms) VI can run continuously for roughly 2 months before it rolls over. The tick count is reset back to zero when you restart your computer, so restarting the computer occasionally can prevent the Tick Count (ms) VI from rolling over.
  • The Get Date/Time In Seconds VI returns the number of seconds that have elapsed since 1904. This VI will not rollover until about 2050.