|
| 1 | +#include "binaryfusefilter.h" |
| 2 | +#include "xorfilter.h" |
| 3 | +#include <assert.h> |
| 4 | +#include <time.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <stdlib.h> |
| 7 | + |
| 8 | +#define N 1000000UL |
| 9 | +#define Q N |
| 10 | + |
| 11 | +static double time_seconds() { |
| 12 | + return (double)clock() / (double)CLOCKS_PER_SEC; |
| 13 | +} |
| 14 | + |
| 15 | +static void run_binaryfuse8() { |
| 16 | + printf("\nRunning binary_fuse8 query benchmark\n"); |
| 17 | + binary_fuse8_t filter; |
| 18 | + if (!binary_fuse8_allocate((uint32_t)N, &filter)) { |
| 19 | + fprintf(stderr, "binary_fuse8_allocate failed\n"); |
| 20 | + return; |
| 21 | + } |
| 22 | + uint64_t *keys = (uint64_t *)malloc(sizeof(uint64_t) * N); |
| 23 | + for (size_t i = 0; i < N; i++) keys[i] = (uint64_t)i * 2ULL; // even numbers |
| 24 | + if (!binary_fuse8_populate(keys, (uint32_t)N, &filter)) { |
| 25 | + fprintf(stderr, "binary_fuse8_populate failed\n"); |
| 26 | + binary_fuse8_free(&filter); |
| 27 | + free(keys); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + // warm up |
| 32 | + for (size_t i = 0; i < 1000; i++) binary_fuse8_contain(keys[i], &filter); |
| 33 | + |
| 34 | + size_t found = 0; |
| 35 | + double t0 = time_seconds(); |
| 36 | + for (size_t i = 0; i < Q; i++) { |
| 37 | + if (binary_fuse8_contain((uint64_t)i, &filter)) found++; |
| 38 | + } |
| 39 | + double t1 = time_seconds(); |
| 40 | + double secs = t1 - t0; |
| 41 | + double qps = (double)Q / secs; |
| 42 | + double ns_per_q = (secs * 1e9) / (double)Q; |
| 43 | + printf("binary_fuse8: %zu queries in %f s => %f q/s, %f ns/q, found=%zu\n", |
| 44 | + (size_t)Q, secs, qps, ns_per_q, found); |
| 45 | + |
| 46 | + binary_fuse8_free(&filter); |
| 47 | + free(keys); |
| 48 | +} |
| 49 | + |
| 50 | +static void run_xor8() { |
| 51 | + printf("\nRunning xor8 query benchmark\n"); |
| 52 | + xor8_t filter; |
| 53 | + if (!xor8_allocate((uint32_t)N, &filter)) { |
| 54 | + fprintf(stderr, "xor8_allocate failed\n"); |
| 55 | + return; |
| 56 | + } |
| 57 | + uint64_t *keys = (uint64_t *)malloc(sizeof(uint64_t) * N); |
| 58 | + for (size_t i = 0; i < N; i++) keys[i] = (uint64_t)i * 2ULL; // even numbers |
| 59 | + if (!xor8_populate(keys, (uint32_t)N, &filter)) { |
| 60 | + fprintf(stderr, "xor8_populate failed\n"); |
| 61 | + xor8_free(&filter); |
| 62 | + free(keys); |
| 63 | + return; |
| 64 | + } |
| 65 | + |
| 66 | + for (size_t i = 0; i < 1000; i++) xor8_contain(keys[i], &filter); |
| 67 | + |
| 68 | + size_t found = 0; |
| 69 | + double t0 = time_seconds(); |
| 70 | + for (size_t i = 0; i < Q; i++) { |
| 71 | + if (xor8_contain((uint64_t)i, &filter)) found++; |
| 72 | + } |
| 73 | + double t1 = time_seconds(); |
| 74 | + double secs = t1 - t0; |
| 75 | + { |
| 76 | + double qps = (double)Q / secs; |
| 77 | + double ns_per_q = (secs * 1e9) / (double)Q; |
| 78 | + printf("xor8: %zu queries in %f s => %f q/s, %f ns/q, found=%zu\n", |
| 79 | + (size_t)Q, secs, qps, ns_per_q, found); |
| 80 | + } |
| 81 | + |
| 82 | + xor8_free(&filter); |
| 83 | + free(keys); |
| 84 | +} |
| 85 | + |
| 86 | +static void run_binaryfuse16() { |
| 87 | + printf("\nRunning binary_fuse16 query benchmark\n"); |
| 88 | + binary_fuse16_t filter; |
| 89 | + if (!binary_fuse16_allocate((uint32_t)N, &filter)) { |
| 90 | + fprintf(stderr, "binary_fuse16_allocate failed\n"); |
| 91 | + return; |
| 92 | + } |
| 93 | + uint64_t *keys = (uint64_t *)malloc(sizeof(uint64_t) * N); |
| 94 | + for (size_t i = 0; i < N; i++) keys[i] = (uint64_t)i * 2ULL; // even numbers |
| 95 | + if (!binary_fuse16_populate(keys, (uint32_t)N, &filter)) { |
| 96 | + fprintf(stderr, "binary_fuse16_populate failed\n"); |
| 97 | + binary_fuse16_free(&filter); |
| 98 | + free(keys); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + for (size_t i = 0; i < 1000; i++) binary_fuse16_contain(keys[i], &filter); |
| 103 | + |
| 104 | + size_t found = 0; |
| 105 | + double t0 = time_seconds(); |
| 106 | + for (size_t i = 0; i < Q; i++) { |
| 107 | + if (binary_fuse16_contain((uint64_t)i, &filter)) found++; |
| 108 | + } |
| 109 | + double t1 = time_seconds(); |
| 110 | + double secs = t1 - t0; |
| 111 | + { |
| 112 | + double qps = (double)Q / secs; |
| 113 | + double ns_per_q = (secs * 1e9) / (double)Q; |
| 114 | + printf("binary_fuse16: %zu queries in %f s => %f q/s, %f ns/q, found=%zu\n", |
| 115 | + (size_t)Q, secs, qps, ns_per_q, found); |
| 116 | + } |
| 117 | + |
| 118 | + binary_fuse16_free(&filter); |
| 119 | + free(keys); |
| 120 | +} |
| 121 | + |
| 122 | +static void run_xor16() { |
| 123 | + printf("\nRunning xor16 query benchmark\n"); |
| 124 | + xor16_t filter; |
| 125 | + if (!xor16_allocate((uint32_t)N, &filter)) { |
| 126 | + fprintf(stderr, "xor16_allocate failed\n"); |
| 127 | + return; |
| 128 | + } |
| 129 | + uint64_t *keys = (uint64_t *)malloc(sizeof(uint64_t) * N); |
| 130 | + for (size_t i = 0; i < N; i++) keys[i] = (uint64_t)i * 2ULL; // even numbers |
| 131 | + if (!xor16_populate(keys, (uint32_t)N, &filter)) { |
| 132 | + fprintf(stderr, "xor16_populate failed\n"); |
| 133 | + xor16_free(&filter); |
| 134 | + free(keys); |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + for (size_t i = 0; i < 1000; i++) xor16_contain(keys[i], &filter); |
| 139 | + |
| 140 | + size_t found = 0; |
| 141 | + double t0 = time_seconds(); |
| 142 | + for (size_t i = 0; i < Q; i++) { |
| 143 | + if (xor16_contain((uint64_t)i, &filter)) found++; |
| 144 | + } |
| 145 | + double t1 = time_seconds(); |
| 146 | + double secs = t1 - t0; |
| 147 | + { |
| 148 | + double qps = (double)Q / secs; |
| 149 | + double ns_per_q = (secs * 1e9) / (double)Q; |
| 150 | + printf("xor16: %zu queries in %f s => %f q/s, %f ns/q, found=%zu\n", |
| 151 | + (size_t)Q, secs, qps, ns_per_q, found); |
| 152 | + } |
| 153 | + |
| 154 | + xor16_free(&filter); |
| 155 | + free(keys); |
| 156 | +} |
| 157 | + |
| 158 | +int main() { |
| 159 | + run_binaryfuse8(); |
| 160 | + run_xor8(); |
| 161 | + run_binaryfuse16(); |
| 162 | + run_xor16(); |
| 163 | + return 0; |
| 164 | +} |
0 commit comments