|
| 1 | +/** |
| 2 | + * @file memory usage benchmarks |
| 3 | + * @author Rafael Kallis <rk@rafaelkallis.com> |
| 4 | + */ |
| 5 | + |
| 6 | +#include "art.hpp" |
| 7 | +#include "zipf.hpp" |
| 8 | +#include <cstdint> |
| 9 | +#include <functional> |
| 10 | +#include <iostream> |
| 11 | +#include <string> |
| 12 | + |
| 13 | +using std::string; |
| 14 | +using std::to_string; |
| 15 | +using std::hash; |
| 16 | + |
| 17 | +// Number of elements to insert for memory benchmarks |
| 18 | +const uint32_t NUM_ELEMENTS = 1000000; |
| 19 | + |
| 20 | +/** |
| 21 | + * Memory benchmark with uniformly distributed keys |
| 22 | + * Uses nullptr values to measure only data structure overhead |
| 23 | + */ |
| 24 | +static void memory_uniform() { |
| 25 | + art::art<int*> m; |
| 26 | + hash<uint32_t> h; |
| 27 | + |
| 28 | + // Fill tree with nullptr values |
| 29 | + for (uint32_t i = 0; i < NUM_ELEMENTS; i++) { |
| 30 | + m.set(to_string(h(i)).c_str(), nullptr); |
| 31 | + } |
| 32 | + |
| 33 | + std::cout << "Inserted " << NUM_ELEMENTS << " elements (uniform distribution)" << std::endl; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Memory benchmark with Zipfian distributed keys |
| 38 | + * Uses nullptr values to measure only data structure overhead |
| 39 | + */ |
| 40 | +static void memory_zipf() { |
| 41 | + art::art<int*> m; |
| 42 | + hash<uint32_t> h; |
| 43 | + fast_zipf rng(NUM_ELEMENTS); |
| 44 | + |
| 45 | + // Fill tree with nullptr values |
| 46 | + for (uint32_t i = 0; i < NUM_ELEMENTS; i++) { |
| 47 | + m.set(to_string(h(rng())).c_str(), nullptr); |
| 48 | + } |
| 49 | + |
| 50 | + std::cout << "Inserted " << NUM_ELEMENTS << " elements (zipfian distribution)" << std::endl; |
| 51 | +} |
| 52 | + |
| 53 | +int main(int argc, char *argv[]) { |
| 54 | + if (argc < 2) { |
| 55 | + std::cerr << "Usage: " << argv[0] << " <uniform|zipf>" << std::endl; |
| 56 | + return 1; |
| 57 | + } |
| 58 | + |
| 59 | + std::string mode(argv[1]); |
| 60 | + |
| 61 | + if (mode == "uniform") { |
| 62 | + memory_uniform(); |
| 63 | + } else if (mode == "zipf") { |
| 64 | + memory_zipf(); |
| 65 | + } else { |
| 66 | + std::cerr << "Unknown mode: " << mode << std::endl; |
| 67 | + std::cerr << "Valid modes: uniform, zipf" << std::endl; |
| 68 | + return 1; |
| 69 | + } |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
0 commit comments