Skip to content

Commit ca47078

Browse files
committed
Implement benchmark_read
1 parent 293228b commit ca47078

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ add_example(mtx2bsp)
1111
add_example(bsp2mtx)
1212
add_example(check_equivalence)
1313
add_example(bsp-ls)
14+
add_example(benchmark_read)

examples/benchmark_read.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <binsparse/binsparse.h>
2+
#include <time.h>
3+
4+
double gettime() {
5+
struct timespec time;
6+
clock_gettime(CLOCK_MONOTONIC, &time);
7+
return ((double) time.tv_sec) + ((double) 1e-9) * time.tv_nsec;
8+
}
9+
10+
int compar(const void* a, const void* b) {
11+
double x = *((const double*) a);
12+
double y = *((const double*) b);
13+
14+
return x - y;
15+
}
16+
17+
int main(int argc, char** argv) {
18+
if (argc < 2) {
19+
fprintf(stderr, "usage: ./benchmark_read [file_name.h5]\n");
20+
return 1;
21+
}
22+
23+
char* file_name = argv[1];
24+
25+
printf("Opening %s\n", file_name);
26+
27+
const int num_trials = 10;
28+
29+
double durations[num_trials];
30+
31+
for (size_t i = 0; i < num_trials; i++) {
32+
double begin = gettime();
33+
bsp_matrix_t mat = bsp_read_matrix(file_name, NULL);
34+
double end = gettime();
35+
durations[i] = end - begin;
36+
bsp_destroy_matrix_t(mat);
37+
}
38+
39+
qsort(durations, num_trials, sizeof(double), compar);
40+
41+
printf("Read file in %lf seconds\n", durations[num_trials / 2]);
42+
43+
printf("[");
44+
for (size_t i = 0; i < num_trials; i++) {
45+
printf("%lf", durations[i]);
46+
if (i + 1 < num_trials) {
47+
printf(", ");
48+
}
49+
}
50+
printf("]\n");
51+
52+
return 0;
53+
}

0 commit comments

Comments
 (0)