Subversion Repositories programming

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
86 irasnyd 1
/* timing.c -- A holder for some functions to find execution time
2
 *
3
 * Written By: Ira Snyder
4
 * Started On: 06-02-2005
5
 *
6
 * License: MIT License
7
 * License: http://www.opensource.org/licenses/mit-license.php
8
 *
9
 * These are used to time the execution time of pieces of code.
10
 * Use like this:
11
 *
12
 * startclock();
13
 * ... run something here ...
14
 * stopclock();
15
 * printtimetaken("SOME NAME");
16
 * 
17
 * And that's it!
18
 */
19
 
20
#ifndef TIMING_C
21
#define TIMING_C
22
 
23
#include <stdio.h> /* for printf() */
24
#include <time.h>  /* for gettimeofday() */
25
 
26
/* global timeval for timing the function calls */
27
struct timeval start_time, end_time;
28
 
29
/* start the clock (for timing) */
30
void startclock (void)
31
{
32
    gettimeofday(&start_time, NULL);
33
}
34
 
35
/* stop the clock (for timing) */
36
void stopclock (void)
37
{
38
    gettimeofday(&end_time, NULL);
39
}
40
 
87 irasnyd 41
/* print the time taken, in milliseconds = seconds/1000 */
86 irasnyd 42
void printtimetaken (char *funcname)
43
{
44
    double total_usecs = (end_time.tv_sec-start_time.tv_sec) * 1000000.0
45
                       + (end_time.tv_usec-start_time.tv_usec);
46
 
47
    printf("%s : %.3lf milliseconds\n", funcname, total_usecs/1000.0);
48
}
49
 
50
#endif