summaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/util/hamming/hamming_benchmark.cpp
blob: 347c935f5b79e25afc46b38be89f18cda6cb1237 (plain) (blame)
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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/vespalib/util/binary_hamming_distance.h>
#include <vector>
#include <cinttypes>
#include <cstdlib>
#include <cstdint>
#include <cstdio>

using namespace vespalib;

int main(int argc, char* argv[]) {
    size_t vector_length = 1024/8;
    size_t num_vectors = 1;
    size_t num_reps = 100000000;

    if (argc > 2) {
        vector_length = atol(argv[2])/8;
    }
    if (argc > 3) {
        num_reps = atol(argv[3]);
    }
    if (argc > 4) {
        num_vectors = atol(argv[4]);
    }

    std::vector<uint8_t> center(vector_length);
    std::vector<uint8_t> vectors(num_vectors*vector_length);
    srand(13);
    for (uint8_t & v : center) { v = rand(); }
    for (uint8_t & v : vectors) { v = rand(); }
    uint64_t sum(0);
    for (size_t i=0; i < num_reps; i++) {
        for (size_t j(0); j < num_vectors; j++) {
            sum += binary_hamming_distance(center.data(), vectors.data() + j*vector_length, vector_length);
        }
    }

    printf("%lu vectors of %lu bits, repeated %lu times. Sum of distances = %" PRIu64 "\n", num_vectors, vector_length*8, num_reps, sum);
    return 0;
}