-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunix_timer.c
83 lines (64 loc) · 2.12 KB
/
unix_timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*
AAAA CCCC OOOO TTTTTT SSSSS PPPPP
AA AA CC OO OO TT SS PP PP
AAAAAA CC OO OO TT SSSS PPPPP
AA AA CC OO OO TT SS PP
AA AA CCCC OOOO TT SSSSS PP
######################################################
########## ACO algorithms for the TSP ##########
######################################################
Version: 1.0
File: unix_timer.c
Author: Thomas Stuetzle
Purpose: routines for measuring elapsed time (CPU or real)
Check: README.txt and legal.txt
*/
#include <stdio.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "timer.h"
static struct rusage res;
static struct timeval tp;
static double virtual_time, real_time;
void start_timers(void)
/*
FUNCTION: virtual and real time of day are computed and stored to
allow at later time the computation of the elapsed time
(virtual or real)
INPUT: none
OUTPUT: none
(SIDE)EFFECTS: virtual and real time are computed
*/
{
getrusage( RUSAGE_SELF, &res );
virtual_time = (double) res.ru_utime.tv_sec +
(double) res.ru_stime.tv_sec +
(double) res.ru_utime.tv_usec / 1000000.0 +
(double) res.ru_stime.tv_usec / 1000000.0;
gettimeofday( &tp, NULL );
real_time = (double) tp.tv_sec +
(double) tp.tv_usec / 1000000.0;
}
double elapsed_time(TIMER_TYPE type)
/*
FUNCTION: return the time used in seconds (virtual or real, depending on type)
INPUT: TIMER_TYPE (virtual or real time)
OUTPUT: seconds since last call to start_timers (virtual or real)
(SIDE)EFFECTS: none
*/
{
if (type == REAL) {
gettimeofday( &tp, NULL );
return( (double) tp.tv_sec +
(double) tp.tv_usec / 1000000.0
- real_time );
}
else {
getrusage( RUSAGE_SELF, &res );
return( (double) res.ru_utime.tv_sec +
(double) res.ru_stime.tv_sec +
(double) res.ru_utime.tv_usec / 1000000.0 +
(double) res.ru_stime.tv_usec / 1000000.0
- virtual_time );
}
}