-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.h
41 lines (30 loc) · 1.04 KB
/
timer.h
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
#ifndef _TIMER_H_
#define _TIMER_H_
#include <stdio.h>
#include <sys/time.h>
typedef struct {
struct timeval startTime;
struct timeval endTime;
float elapsedTime;
} Timer;
static void startTime(Timer* timer) {
gettimeofday(&(timer->startTime), NULL);
}
static void stopTime(Timer* timer) {
gettimeofday(&(timer->endTime), NULL);
timer->elapsedTime = ((float) ((timer->endTime.tv_sec - timer->startTime.tv_sec)
+ (timer->endTime.tv_usec - timer->startTime.tv_usec)/1.0e6));
}
static void printElapsedTime(Timer timer, const char* label) {
printf("%s: %f s\n", label, timer.elapsedTime);
}
static void stopTimeAndPrint(Timer* timer, const char* label) {
stopTime(timer);
printElapsedTime(*timer, label);
}
static void stopTimeAndPrintWithRate(Timer* timer, const char* timeLabel, const char* rateLabel, unsigned int units) {
stopTime(timer);
float rate = units / timer->elapsedTime;
printf("%s: %f s (%f %s/s)\n", timeLabel, timer->elapsedTime, rate, rateLabel);
}
#endif