-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.c
92 lines (74 loc) · 2.12 KB
/
benchmark.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
81
82
83
84
85
86
87
88
89
90
91
92
#include <time.h>
#include <stdio.h>
#include "benchmark.h"
#ifdef WIN32
#include <windows.h>
double get_time() {
LARGE_INTEGER t, f;
QueryPerformanceCounter(&t);
QueryPerformanceFrequency(&f);
return (double) t.QuadPart / (double) f.QuadPart;
}
#else
#include <sys/time.h>
double get_time() {
struct timespec t;
clock_gettime(CLOCK_MONOTONIC, &t);
return (double) t.tv_sec + 1.0e-9 * t.tv_nsec;
}
#endif
void benchmark(
char *name,
Graph graph,
void (*dijkstra)(Graph, size_t[]),
bool print_result,
size_t warmup_amount,
size_t measure_amount
) {
size_t costs[graph->vertices];
// Warmup
for (int i = 0; i < warmup_amount; i++)
dijkstra(graph, costs);
double measures[measure_amount];
// Measure
printf("Benchmark %s (%zu, %zu):", name, graph->vertices, graph->edges);
for (int i = 0; i < measure_amount; i++) {
double start_time = get_time();
dijkstra(graph, costs);
measures[i] = get_time() - start_time;
}
for (int i = 0; i < measure_amount; i++)
printf(" %2.8f", measures[i]);
printf("\n");
}
void benchmark2(
char *name,
Graph graph,
void (*dijkstra)(Graph, size_t[]),
bool print_result,
size_t warmup_amount,
size_t measure_amount
) {
size_t costs[graph->vertices];
// Warmup
for (int i = 0; i < warmup_amount; i++)
dijkstra(graph, costs);
double measures[measure_amount];
// Measure
printf("Benchmark %s (%zu, %zu):", name, graph->vertices, graph->edges);
for (int i = 0; i < measure_amount; i++) {
double start_time = get_time();
dijkstra(graph, costs);
measures[i] = get_time() - start_time;
// Print the last result
if (print_result && i == measure_amount - 1) {
printf("Results:\n");
for (size_t j = 0; j < graph->vertices; j++)
printf("%zu %zu\n", j, costs[j]);
printf("End results");
}
}
for (int i = 0; i < measure_amount; i++)
printf(" %2.8f", measures[i]);
printf("\n");
}