-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathktiming.cpp
executable file
·109 lines (92 loc) · 3.49 KB
/
ktiming.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/**
* Copyright (c) 2012 MIT License by 6.172 Staff
* Copyright (c) 2018 I-Ting Angelina Lee
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
**/
/**
* Linux kernel-assisted timing library -- provides high-precision time
* measurements for the execution time of your algorithms.
*
* You shouldn't need to modify this file. More importantly, you should not
* depend on any modifications you make here, as we will replace it with a
* fresh copy when we test your code.
**/
#include "./ktiming.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define USEC_TO_SEC(x) ((float)x*1.0e-9)
clockmark_t ktiming_getmark(void) {
struct timespec temp;
uint64_t nanos;
int stat = clock_gettime(CLOCK_MONOTONIC , &temp);
if (stat != 0) {
perror("ktiming_getmark()");
exit(-1);
}
nanos = temp.tv_nsec;
nanos += ((uint64_t)temp.tv_sec) * 1000 * 1000 * 1000;
return nanos;
}
uint64_t ktiming_diff_usec(const clockmark_t* const
start, const clockmark_t* const end) {
return *end - *start;
}
float ktiming_diff_sec(const clockmark_t* const start,
const clockmark_t* const end) {
return (float) ktiming_diff_usec(start, end) / 1000000000.0f;
}
static void
print_runtime_helper(uint64_t *usec_elapsed, int size, int summary) {
int i;
uint64_t total = 0, diff = 0;
double ave, std_dev = 0, dev_sq_sum = 0;
for (i = 0; i < size; i++) {
total += usec_elapsed[i];
if(!summary) {
printf("Running time %d: %4lf s\n", (i + 1),
USEC_TO_SEC(usec_elapsed[i]));
}
}
ave = total / size;
if( size > 1 ) {
for (i = 0; i < size; i++) {
if(ave > usec_elapsed[i]) { diff = ave - usec_elapsed[i]; }
else { diff = usec_elapsed[i] - ave; }
dev_sq_sum += ( USEC_TO_SEC(diff) * USEC_TO_SEC(diff) );
}
std_dev = sqrt(dev_sq_sum / (size-1));
}
printf( "Running time average: %4lf s\n", USEC_TO_SEC(ave) );
if( std_dev != 0 ) {
printf( "Std. dev: %g s (%2.3f%%)\n",
std_dev, 100.0*(std_dev/USEC_TO_SEC(ave)) );
}
}
void print_runtime(uint64_t *tm_elapsed, int size) {
print_runtime_helper(tm_elapsed, size, 0);
}
void print_runtime_summary(uint64_t *tm_elapsed, int size) {
print_runtime_helper(tm_elapsed, size, 1);
}
void print_single_runtime(uint64_t tm_elapsed) {
printf("Running time: %4lf s\n", USEC_TO_SEC(tm_elapsed));
}