Listing 1.
elapsed c: a C function to be wrappered by SWIG
Scott Bolte

SWIG
The Perl Journal, Winter 1997
 

#include <sys/time.h>

/* 

   Return the number of seconds since the first time 

   elapsed_seconds() was called.

 */



float

elapsed_seconds(void)

{

    	static struct timeval then = {0, 0};

    	struct timeval  now;



    	/* The first time we're called note the time 

           and then return 0.

    	 */

    	if (then.tv_sec == 0) {

               (void) gettimeofday(&then, 0);

               return (0);

    	}



    	/*

           Return the elapsed time as a floating point 

           number on subsequent calls.

    	 */

    	(void) gettimeofday(&now, 0);

    	return (now.tv_sec - then.tv_sec

             + (now.tv_usec - then.tv_usec) / 1000000.0);

}