Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Performace test for UUID comparison.
- *
- * $ g++ ./main.cpp -O2 -DNDEBUG && ./a.out
- * uint32_t aligned: 1180.718088 ms
- * memcmp aligned: 488.283732 ms
- * uint32_t unaligned: 1225.530747 ms
- * memcmp unaligned: 534.292165 ms
- */
- #include <time.h>
- #include <climits>
- #include <cstdint>
- #include <cstdio>
- #include <cstring>
- #include <vector>
- struct Test
- {
- uint32_t a;
- uint16_t b;
- uint16_t c;
- uint8_t d;
- uint8_t e;
- uint8_t f[6];
- };
- const size_t N = 32 * 1024 * 1024;
- const size_t M = 10;
- Test data1[N];
- Test data2[N];
- char raw1[N * sizeof(Test) + 1];
- char raw2[N * sizeof(Test) + 1];
- uint64_t now()
- {
- struct timespec ts;
- clock_gettime(CLOCK_MONOTONIC, &ts);
- return ((uint64_t)ts.tv_sec) * 1000000000 + ts.tv_nsec;
- }
- bool eq_uint32(const Test *a, const Test *b)
- {
- uint32_t *pa = (uint32_t*) a;
- uint32_t *pb = (uint32_t*) b;
- return pa[0] == pb[0] && pa[1] == pb[1] && pa[2] == pb[2] && pa[3] == pb[3];
- }
- bool eq_memcmp(const Test *a, const Test *b)
- {
- return memcmp(a, b, sizeof(Test)) == 0;
- }
- int __attribute__((noinline)) test_uint32(const Test *data1, const Test *data2)
- {
- int res = 0;
- for (size_t i = 0; i < N; i++)
- if (!eq_uint32(data1 + i, data2 + i))
- ++res;
- return res;
- }
- int __attribute__((noinline)) test_memcmp(const Test *data1, const Test *data2)
- {
- int res = 0;
- for (size_t i = 0; i < N; i++)
- if (!eq_memcmp(data1 + i, data2 + i))
- ++res;
- return res;
- }
- int main(int n, const char**)
- {
- const char* names[] = {"uint32_t aligned: ", "memcmp aligned: ", "uint32_t unaligned: ", "memcmp unaligned: "};
- int (*test[2])(const Test *, const Test *) = {test_uint32, test_memcmp};
- int rc = 0;
- for (size_t k = 0; k < 4; k++)
- {
- const Test* d1 = (k >> 1) == 0 ? data1 : (const Test*)(raw1 + 1);
- const Test* d2 = (k >> 1) == 0 ? data2 : (const Test*)(raw2 + 1);
- uint64_t t = now();
- for (size_t i = 0; i < M; i++)
- rc += test[k & 1](d1, d2);
- t = now() - t;
- printf("%s\t %lf ms\n", names[k], t / 1000000.);
- }
- return rc;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement