Alien-Judy

 view release on metacpan or  search on metacpan

src/judy-1.0.5/test/timeit.c  view on Meta::CPAN


	return (DeltaUSec / ((double) (__stop_tm - __start_tm)));

} // find_CPU_speed()

#else // _TIMEIT_HIGHRES
void dummy() {}	 // avoid "empty source file" warnings when no _TIMEIT_TEST.
#endif


// ****************************************************************************
//
// Ifdef the test main() separately, including #includes, for platforms that do
// not define find_CPU_speed() above.

#ifdef _TIMEIT_TEST

#include <sys/time.h>		// Win32 uses a whole different paradigm.
#include <unistd.h>		// for getopt(), which Win32 lacks.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
//#include <values.h>             // for MAXDOUBLE
#define MAXDOUBLE       (10e99)
#include "timeit.h"


// ****************************************************************************
// M A I N
//
// Example code for timeit:
//
// To compile and test this program on HP-UX, run the next lines as commands.
//
//   cc -Wl,-a,archive -DJU_HPUX_PA -D__HPUX__ -D_TIMEIT_TEST -o timeit timeit.c
//   timeit			# run test program.
//   rm -f timeit		# clean up after test.

int main(int argc, char **argv)
{
	int     i = 0;		// loop index.
	long    i_max = 10;	// number of loops.
	int     preload = 1;	// loops to throw away (preload cache).
	double  ztime;		// timer overhead.
	double  usec[4];	// for early timing tests.
	double  DeltaUSec;	// timing result in usec.
	double  prevtime;	// from previous loop.
	double  mintime;	// minimum event time.
	struct timeval tmjunk;	// for throw-away syscall.
	TIMER_vars(tm1);	// overall timer variables.
	TIMER_vars(tm2);	// misc + loop timer variables.


// INITIALIZE:

	STARTTm(tm1);		// whole program timer.

	i_max += preload;

// The first arg is the number of iterations (default is i_max):

	if (argc > 1)
	{
	    i = atoi(argv[1]) + preload;
	    if (i > 0) i_max = (long)i;
	}

// Calculate timer overhead (ztime):

#ifdef _TIMEIT_HIGHRES
	(void) puts("Possible slight delay here due to find_CPU_speed()...");
#else
	(void) puts("No high-res clock or find_CPU_speed() for this platform.");
#endif

	ztime = 0.0;

	for (i = 0; i < 100; ++i)	// average many runs.
	{
	    STARTTm(tm2);
	    ENDTm(DeltaUSec, tm2);
	    ztime += DeltaUSec;
	}
	ztime = ztime / ((double) i);


// SIMPLE TESTS OF TIMER OVERHEAD:
//
// Make two passes at both the high-res (if any) and slower timers.

	(void) puts("\nTiming timers themselves: start, end, end");

#define	PRINTPASS(Desc,Pass)						\
	(void) printf("%-8s pass %d:  %f - %f = %f usec\n", Desc, Pass,	\
		      usec[((Pass) * 2) - 1],  usec[((Pass) * 2) - 2],	\
		      usec[((Pass) * 2) - 1] - usec[((Pass) * 2) - 2])

#ifdef _TIMEIT_HIGHRES
	START_HRTm(tm2);
	END_HRTm(usec[0], tm2);	// throw away in case of sleep(1) here.

	START_HRTm(tm2);
	END_HRTm(usec[0], tm2);
	END_HRTm(usec[1], tm2);

	START_HRTm(tm2);
	END_HRTm(usec[2], tm2);
	END_HRTm(usec[3], tm2);

	PRINTPASS("High-res", 1);
	PRINTPASS("High-res", 2);
#endif

	STARTTm(tm2);
	ENDTm(usec[0], tm2);	// throw away in case of sleep(1) here.

	STARTTm(tm2);
	ENDTm(usec[0], tm2);
	ENDTm(usec[1], tm2);



( run in 0.667 second using v1.01-cache-2.11-cpan-71847e10f99 )