LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to determine elapsed time?

How does one determine elapsed time of a chunk of code?

I've looked through all of my CVI 7.1 functions, and cannot find anything that will read the processor tick count. GetTickCount() is not available without Windows32.h and presumably the dll, if the function is not resident. I don't have Windows32.h, or know how to get it.

Any ideas?
0 Kudos
Message 1 of 4
(4,776 Views)
Precisely what are you trying to measure?

If you just want elapsed test time in seconds, you can use the ANSI C time() and difftime() functions to do this.

This is pretty simple to do, something like this will work:


void show_elapsed_time (time_t TestStart)
{
// Call to calculate and display elapsed test time

int hours, minutes, seconds;
char time_str[10];
time_t now, elapsed;

time (&now);
elapsed = difftime (now, TestStart);

hours = (int) (elapsed / 3600);
elapsed -= (hours * 3600);
minutes = (int) (elapsed / 60);
seconds = (int) (elapsed - (minutes * 60));

sprintf (time_str, "%0d:%0d:%0d", hours, minutes, seconds);
SetCtrlVal (mainpanel, MAINPANEL_TEST_TIME, time_str);
}

If you want to determine execution time for a block of code, that is more difficult. You can do it with SDK functions. If you want the SDK functions you will need to get the full development version of CVI. You can download a generic version of the Platform SDK directly from MSDN but it will not have the special tweaks that NI has added to the CVI version and probably won't be worth the time and effort you would have to put into using it.
Martin Fredrickson
Test Engineer

Northrop Grumman
Advanced Systems and Products
San Diego, CA 92128
0 Kudos
Message 2 of 4
(4,769 Views)
The ANSI C function time() returns seconds.
The CVI utility function Timer() returns milliseconds.
Under Windows, milliseconds is the finest resolution you can get in software. If you need finer resolution than that, you need a hardware timer. A finer resolution is not very meaningful because under Windows the time for a chunk of code to execute may vary widely based on what else Windows is doing.
0 Kudos
Message 3 of 4
(4,758 Views)
Thank you Al S, Timer() was what I was looking for. Yes, I need millisecond accuracy on a chunk of code that is executed in a separate thread repeatedly. This appears to work fine. I kept looking for GetTimerCount, but missed this one.
0 Kudos
Message 4 of 4
(4,749 Views)