Advertisement
Guest User

Performace test for UUID comparison

a guest
May 19th, 2020
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. /*
  2.  * Performace test for UUID comparison.
  3.  *
  4.  * $ g++ ./main.cpp -O2 -DNDEBUG && ./a.out
  5.  * uint32_t aligned:     1180.718088 ms
  6.  * memcmp aligned:       488.283732 ms
  7.  * uint32_t unaligned:   1225.530747 ms
  8.  * memcmp unaligned:     534.292165 ms
  9.  */
  10. #include <time.h>
  11.  
  12. #include <climits>
  13. #include <cstdint>
  14. #include <cstdio>
  15. #include <cstring>
  16. #include <vector>
  17.  
  18. struct Test
  19. {
  20.     uint32_t a;
  21.     uint16_t b;
  22.     uint16_t c;
  23.     uint8_t d;
  24.     uint8_t e;
  25.     uint8_t f[6];
  26. };
  27.  
  28. const size_t N = 32 * 1024 * 1024;
  29. const size_t M = 10;
  30.  
  31. Test data1[N];
  32. Test data2[N];
  33.  
  34. char raw1[N * sizeof(Test) + 1];
  35. char raw2[N * sizeof(Test) + 1];
  36.  
  37. uint64_t now()
  38. {
  39.     struct timespec ts;
  40.     clock_gettime(CLOCK_MONOTONIC, &ts);
  41.     return ((uint64_t)ts.tv_sec) * 1000000000 + ts.tv_nsec;
  42. }
  43.  
  44. bool eq_uint32(const Test *a, const Test *b)
  45. {
  46.     uint32_t *pa = (uint32_t*) a;
  47.     uint32_t *pb = (uint32_t*) b;
  48.     return pa[0] == pb[0] && pa[1] == pb[1] && pa[2] == pb[2] && pa[3] == pb[3];
  49. }
  50.  
  51. bool eq_memcmp(const Test *a, const Test *b)
  52. {
  53.     return memcmp(a, b, sizeof(Test)) == 0;
  54. }
  55.  
  56. int __attribute__((noinline)) test_uint32(const Test *data1, const Test *data2)
  57. {
  58.     int res = 0;
  59.     for (size_t i = 0; i < N; i++)
  60.         if (!eq_uint32(data1 + i, data2 + i))
  61.             ++res;
  62.     return res;
  63. }
  64.  
  65.  
  66. int __attribute__((noinline)) test_memcmp(const Test *data1, const Test *data2)
  67. {
  68.     int res = 0;
  69.     for (size_t i = 0; i < N; i++)
  70.         if (!eq_memcmp(data1 + i, data2 + i))
  71.             ++res;
  72.     return res;
  73. }
  74.  
  75. int main(int n, const char**)
  76. {
  77.     const char* names[] = {"uint32_t aligned: ", "memcmp aligned:  ", "uint32_t unaligned: ", "memcmp unaligned: "};
  78.     int (*test[2])(const Test *, const Test *) = {test_uint32, test_memcmp};
  79.  
  80.     int rc = 0;
  81.     for (size_t k = 0; k < 4; k++)
  82.     {
  83.         const Test* d1 = (k >> 1) == 0 ? data1 : (const Test*)(raw1 + 1);
  84.         const Test* d2 = (k >> 1) == 0 ? data2 : (const Test*)(raw2 + 1);
  85.         uint64_t t = now();
  86.         for (size_t i = 0; i < M; i++)
  87.             rc += test[k & 1](d1, d2);
  88.         t = now() - t;
  89.         printf("%s\t %lf ms\n", names[k], t / 1000000.);
  90.     }
  91.     return rc;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement