Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
141 Kevin 1
/*
2
 * ftimer.c - Estimate the time (in seconds) used by a function f 
3
 * 
4
 * Copyright (c) 2002, R. Bryant and D. O'Hallaron, All rights reserved.
5
 * May not be used, modified, or copied without permission.
6
 *
7
 * Function timers that estimate the running time (in seconds) of a function f.
8
 *    ftimer_itimer: version that uses the interval timer
9
 *    ftimer_gettod: version that uses gettimeofday
10
 */
11
#include <stdio.h>
12
#include <sys/time.h>
13
#include "ftimer.h"
14
 
15
/* function prototypes */
16
static void init_etime(void);
17
static double get_etime(void);
18
 
19
/* 
20
 * ftimer_itimer - Use the interval timer to estimate the running time
21
 * of f(argp). Return the average of n runs.  
22
 */
23
double ftimer_itimer(ftimer_test_funct f, void *argp, int n)
24
{
25
    double start, tmeas;
26
    int i;
27
 
28
    init_etime();
29
    start = get_etime();
30
    for (i = 0; i < n; i++) 
31
	f(argp);
32
    tmeas = get_etime() - start;
33
    return tmeas / n;
34
}
35
 
36
/* 
37
 * ftimer_gettod - Use gettimeofday to estimate the running time of
38
 * f(argp). Return the average of n runs.  
39
 */
40
double ftimer_gettod(ftimer_test_funct f, void *argp, int n)
41
{
42
    int i;
43
    struct timeval stv, etv;
44
    double diff;
45
 
46
    gettimeofday(&stv, NULL);
47
    for (i = 0; i < n; i++) 
48
	f(argp);
49
    gettimeofday(&etv,NULL);
50
    diff = 1E3*(etv.tv_sec - stv.tv_sec) + 1E-3*(etv.tv_usec-stv.tv_usec);
51
    diff /= n;
52
    return (1E-3*diff);
53
}
54
 
55
 
56
/*
57
 * Routines for manipulating the Unix interval timer
58
 */
59
 
60
/* The initial value of the interval timer */
61
#define MAX_ETIME 86400   
62
 
63
/* static variables that hold the initial value of the interval timer */
64
static struct itimerval first_u; /* user time */
65
static struct itimerval first_r; /* real time */
66
static struct itimerval first_p; /* prof time*/
67
 
68
/* init the timer */
69
static void init_etime(void)
70
{
71
    first_u.it_interval.tv_sec = 0;
72
    first_u.it_interval.tv_usec = 0;
73
    first_u.it_value.tv_sec = MAX_ETIME;
74
    first_u.it_value.tv_usec = 0;
75
    setitimer(ITIMER_VIRTUAL, &first_u, NULL);
76
 
77
    first_r.it_interval.tv_sec = 0;
78
    first_r.it_interval.tv_usec = 0;
79
    first_r.it_value.tv_sec = MAX_ETIME;
80
    first_r.it_value.tv_usec = 0;
81
    setitimer(ITIMER_REAL, &first_r, NULL);
82
 
83
    first_p.it_interval.tv_sec = 0;
84
    first_p.it_interval.tv_usec = 0;
85
    first_p.it_value.tv_sec = MAX_ETIME;
86
    first_p.it_value.tv_usec = 0;
87
    setitimer(ITIMER_PROF, &first_p, NULL);
88
}
89
 
90
/* return elapsed real seconds since call to init_etime */
91
static double get_etime(void) {
92
    struct itimerval v_curr;
93
    struct itimerval r_curr;
94
    struct itimerval p_curr;
95
 
96
    getitimer(ITIMER_VIRTUAL, &v_curr);
97
    getitimer(ITIMER_REAL,&r_curr);
98
    getitimer(ITIMER_PROF,&p_curr);
99
 
100
    return (double) ((first_p.it_value.tv_sec - r_curr.it_value.tv_sec) +
101
		     (first_p.it_value.tv_usec - r_curr.it_value.tv_usec)*1e-6);
102
}
103
 
104
 
105
 
106