1 | #define _GNU_SOURCE |
2 | #include <roaring/roaring.h> |
3 | #include <inttypes.h> |
4 | #include "benchmark.h" |
5 | #include "numbersfromtextfiles.h" |
6 | |
7 | void iterate_using_advance(roaring_bitmap_t* bm) { |
8 | roaring_uint32_iterator_t *iter = roaring_create_iterator(bm); |
9 | uint64_t sum = 0; |
10 | while (iter->has_value) { |
11 | sum += iter->current_value; |
12 | roaring_advance_uint32_iterator(iter); |
13 | } |
14 | roaring_free_uint32_iterator(iter); |
15 | *(volatile uint64_t*)(&sum) = sum; |
16 | } |
17 | |
18 | void iterate_using_read(roaring_bitmap_t* bm, uint32_t bufsize) { |
19 | uint32_t* buffer = malloc(sizeof(uint32_t) * bufsize); |
20 | roaring_uint32_iterator_t *iter = roaring_create_iterator(bm); |
21 | uint64_t sum = 0; |
22 | while (1) { |
23 | uint32_t ret = roaring_read_uint32_iterator(iter, buffer, bufsize); |
24 | for (uint32_t i = 0; i < ret; i++) { |
25 | sum += buffer[i]; |
26 | } |
27 | if (ret < bufsize) { |
28 | break; |
29 | } |
30 | } |
31 | roaring_free_uint32_iterator(iter); |
32 | free(buffer); |
33 | *(volatile uint64_t*)(&sum) = sum; |
34 | } |
35 | |
36 | int main(int argc, char* argv[]) { |
37 | (void)&read_all_integer_files; // suppress unused warning |
38 | |
39 | if (argc < 2) { |
40 | printf("Usage: %s <comma_separated_integers_file> ...\n" , argv[0]); |
41 | printf("Example: %s ~/CRoaring/benchmarks/realdata/weather_sept_85/*\n" , argv[0]); |
42 | return 1; |
43 | } |
44 | |
45 | roaring_bitmap_t* bm = roaring_bitmap_create(); |
46 | for (int i = 1; i < argc; i++) { |
47 | size_t count = 0; |
48 | uint32_t* values = read_integer_file(argv[i], &count); |
49 | if (count == 0) { |
50 | printf("No integers found in %s\n" , argv[i]); |
51 | return 1; |
52 | } |
53 | //roaring_bitmap_add_many(bm, count, values); |
54 | for (size_t j = 0; j < count; j++) { |
55 | roaring_bitmap_add(bm, values[j]); |
56 | } |
57 | free(values); |
58 | } |
59 | //roaring_bitmap_run_optimize(bm); |
60 | |
61 | printf("Data:\n" ); |
62 | printf(" cardinality: %" PRIu64"\n" , roaring_bitmap_get_cardinality(bm)); |
63 | |
64 | printf("Cycles/element:\n" ); |
65 | uint64_t cycles_start, cycles_final; |
66 | const int num_passes = 5; |
67 | |
68 | printf(" roaring_advance_uint32_iterator():" ); |
69 | for (int p = 0; p < num_passes; p++) { |
70 | RDTSC_START(cycles_start); |
71 | iterate_using_advance(bm); |
72 | RDTSC_FINAL(cycles_final); |
73 | printf(" %f" , (cycles_final - cycles_start) * 1.0 / roaring_bitmap_get_cardinality(bm)); |
74 | } |
75 | printf("\n" ); |
76 | |
77 | const uint32_t bufsizes[] = {1,4,16,128,1024}; |
78 | for (size_t j = 0; j < sizeof(bufsizes)/sizeof(bufsizes[0]); j++) { |
79 | uint32_t bufsize = bufsizes[j]; |
80 | printf(" roaring_read_uint32_iterator(bufsize=%u):" , bufsize); |
81 | for (int p = 0; p < num_passes; p++) { |
82 | RDTSC_START(cycles_start); |
83 | |
84 | iterate_using_read(bm, bufsize); |
85 | |
86 | RDTSC_FINAL(cycles_final); |
87 | printf(" %f" , (cycles_final - cycles_start) * 1.0 / roaring_bitmap_get_cardinality(bm)); |
88 | } |
89 | printf("\n" ); |
90 | } |
91 | |
92 | roaring_bitmap_free(bm); |
93 | return 0; |
94 | } |
95 | |
96 | |