Subversion Repositories programming

Rev

Rev 86 | Blame | Last modification | View Log | RSS feed

/* timing.c -- A holder for some functions to find execution time
 *
 * Written By: Ira Snyder
 * Started On: 06-02-2005
 *
 * License: MIT License
 * License: http://www.opensource.org/licenses/mit-license.php
 *
 * These are used to time the execution time of pieces of code.
 * Use like this:
 *
 * startclock();
 * ... run something here ...
 * stopclock();
 * printtimetaken("SOME NAME");
 * 
 * And that's it!
 */

#ifndef TIMING_C
#define TIMING_C

#include <stdio.h> /* for printf() */
#include <time.h>  /* for gettimeofday() */

/* global timeval for timing the function calls */
struct timeval start_time, end_time;

/* start the clock (for timing) */
void startclock (void)
{
    gettimeofday(&start_time, NULL);
}

/* stop the clock (for timing) */
void stopclock (void)
{
    gettimeofday(&end_time, NULL);
}

/* print the time taken, in milliseconds = seconds/1000 */
void printtimetaken (char *funcname)
{
    double total_usecs = (end_time.tv_sec-start_time.tv_sec) * 1000000.0
                       + (end_time.tv_usec-start_time.tv_usec);

    printf("%s : %.3lf milliseconds\n", funcname, total_usecs/1000.0);
}

#endif