fork download
  1. #include <stdint.h>
  2.  
  3. template<int BytesCount>
  4. struct rawdata
  5. {
  6. char _[ BytesCount ];
  7. inline char& operator[](int index)
  8. {
  9. return _[ index ];
  10. }
  11. };
  12.  
  13. //...
  14.  
  15. rawdata<4> genINC(rawdata<4> p)
  16. {
  17. rawdata<4> r = { p[ 0 ], p[ 1 ], p[ 2 ], p[ 3 ] };
  18. ++*( ( int* )&r[ 0 ] );
  19. return r;
  20. }
  21.  
  22. rawdata<4> genINC32(rawdata<4> p)
  23. {
  24. rawdata<4> res = p;
  25. uint32_t* a = (uint32_t*)&res[0];
  26. ++*a;
  27. return res;
  28. }
  29.  
  30.  
  31. #include <iostream>
  32. #include <chrono>
  33.  
  34. template<int n>
  35. std::ostream& operator<<(std::ostream& os, rawdata<n> p)
  36. {
  37. for(int i = 0; i < n; i++)
  38. os << " - " << (int)p[i];
  39. return os;
  40. }
  41.  
  42. int main()
  43. {
  44. // Check that functions outputs the same things
  45. rawdata<4> test = { 1, 0, 0, 0 };
  46. rawdata<4> t1 = genINC(test);
  47. rawdata<4> t2 = genINC32(test);
  48. std::cout << "test" << test << "\n";
  49. std::cout << "t1 " << t1 << "\n";
  50. std::cout << "t2 " << t2 << "\n";
  51.  
  52. // Perf check
  53. const uint64_t N = 10000000;
  54. rawdata<4> val = test;
  55. std::chrono::steady_clock::time_point start, end;
  56. uint64_t timeINC = 0, timeINC32 = 0;
  57.  
  58. val[0] = 42;
  59. start = std::chrono::steady_clock::now();
  60. for(uint64_t t = 0; t < N; t++)
  61. val = genINC(val);
  62. end = std::chrono::steady_clock::now();
  63. timeINC = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
  64.  
  65. val[0] = 42;
  66. start = std::chrono::steady_clock::now();
  67. for(uint64_t t = 0; t < N; t++)
  68. val = genINC32(val);
  69. end = std::chrono::steady_clock::now();
  70. timeINC32 = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count();
  71.  
  72. std::cout << "INC : " << timeINC << "µs\n";
  73. std::cout << "INC32: " << timeINC32 << "µs\n";
  74. std::cout << "INC32 is " << (timeINC / float(timeINC32)) << "x faster than INC\n";
  75. std::cout << "\n\n(unused" << val << ")\n";
  76. }
  77.  
Success #stdin #stdout 0.09s 3340KB
stdin
Standard input is empty
stdout
test - 1 - 0 - 0 - 0
t1   - 2 - 0 - 0 - 0
t2   - 2 - 0 - 0 - 0
INC  : 39554µs
INC32: 51219µs
INC32 is 0.772253x faster than INC


(unused - -86 - 44 - 49 - 1)