10-14-2010 01:18 PM
Hi all
I have a very simple question. If i want to calculate the time required for one iteration
in a program then how can i do it? Please show it eith an example.
10-14-2010 02:14 PM - edited 10-14-2010 02:17 PM
How accurately do you want to measure the time?
CVI's Timer() function returns the time from the last time it was called.
Here's the CVI help on Timer()
Returns the number of seconds elapsed since the first call to Timer, Delay, or SyncWait, or the first operation on a timer control.
The value is never reset to zero except when you restart the program.
The resolution is normally 1 microsecond. However, if you set the useDefaultTimer configuration option to True, the resolution is 55 milliseconds.
Here's a simple program that uses it.
#include <utility.h>
#include <ansi_c.h>
main()
{
double startTime, endTime;
int i;
startTime = Timer();
for (i=0; i<5; i++)
{
Delay (1);
endTime = Timer();
printf("Time in loop = %f seconds.\n", endTime - startTime);
startTime = endTime;
}
printf("Press any key to continue...\n");
GetKey();
}
10-14-2010 02:17 PM - edited 10-14-2010 02:19 PM
Part of my post keeps getting cut off.
Here's a simple program that uses Timer().
#include <utility.h>
#include <ansi_c.h>
main()
{
double startTime, endTime;
int i;
startTime = Timer();
for (i=0; i<5; i++)
{
Delay (1);
endTime = Timer();
printf("Time in loop = %f seconds.\n", endTime - startTime);
startTime = endTime;
}
printf("Press any key to continue...\n");
GetKey();
}
10-14-2010 02:56 PM
No ready-to-use code, but a simple sketch and two comments:
First, you should not rely on one interation only but call the code many times, ~100 or 1000 repetitions should do for sufficient statistics (Windows is not a RT OS and processing time may change dramatically).
Second, put your code of interest in a timer callback and set the timer interval to zero (i.e. run as fast as possible), using SetCtrlAttribute (panel, PANEL_TIMER, ATTR_INTERVAL, 0.0 );
something like
int CVICALLBACK TimerCallback ( int panel...),
{
switch ( event )
{
case EVENT_TIMER_TICK:
// call your code of interest here
time = *( ( double * ) eventData1 ); // current time
interval = *( ( double * ) eventData2 ); // time elapsed since the last EVENT_TIMER_TICK callback.