ccan/crc32c: new module for accelerated CRC32 (on x86-64).
[ccan] / ccan / crc32c / benchmark / bench.c
1 #include <ccan/tal/grab_file/grab_file.h>
2 #include <ccan/tal/tal.h>
3 #include <ccan/time/time.h>
4 #include <ccan/crc32c/crc32c.c>
5 #include <assert.h>
6 #include <ccan/err/err.h>
7
8 #define RUNS 65536
9
10 int main(int argc, char *argv[])
11 {
12         void *p;
13         struct timeabs start, end;
14         size_t len, runs;
15         uint64_t sums = 0;
16         bool sw = false, hw = false;
17
18         if (argv[1]) {
19                 if (streq(argv[1], "--software")) {
20                         sw = true;
21                         argv++;
22                         argc--;
23
24                 } else if (streq(argv[1], "--hardware")) {
25                         hw = true;
26                         argv++;
27                         argc--;
28                 }
29         }
30
31         if (argc < 2 || (runs = atol(argv[1])) == 0)
32                 errx(1, "Usage: bench <num-runs> [<file>]");
33
34         p = grab_file(NULL, argv[2]);
35         if (!p)
36                 err(1, "Reading %s", argv[2] ? argv[2] : "<stdin>");
37         len = tal_count(p) - 1;
38         start = time_now();
39         if (sw) {
40                 for (size_t i = 0; i < runs; i++)
41                         sums += crc32c_sw(0, p, len);
42         } else if (hw) {
43                 for (size_t i = 0; i < runs; i++)
44                         sums += crc32c_hw(0, p, len);
45         } else {
46                 for (size_t i = 0; i < runs; i++)
47                         sums += crc32c(0, p, len);
48         }
49         end = time_now();
50
51         assert(sums % runs == 0);
52         printf("%u usec for %zu bytes, sum=%08x\n",
53                (int)time_to_usec(time_divide(time_between(end, start), runs)),
54                len,
55                (unsigned int)(sums / runs));
56         return 0;
57 }