fork(4) download
  1. #include <iostream>
  2. #include <chrono>
  3. #include <unordered_map>
  4. #include <algorithm>
  5. #include <random>
  6.  
  7. using namespace std;
  8.  
  9. int hextoint1 (char number)
  10. {
  11. if (number == '0')
  12. return 0;
  13. if (number == '1')
  14. return 1;
  15. if (number == '2')
  16. return 2;
  17. if (number == '3')
  18. return 3;
  19. if (number == '4')
  20. return 4;
  21. if (number == '5')
  22. return 5;
  23. if (number == '6')
  24. return 6;
  25. if (number == '7')
  26. return 7;
  27. if (number == '8')
  28. return 8;
  29. if (number == '9')
  30. return 9;
  31. if (number == 'a')
  32. return 10;
  33. if (number == 'b')
  34. return 11;
  35. if (number == 'c')
  36. return 12;
  37. if (number == 'd')
  38. return 13;
  39. if (number == 'e')
  40. return 14;
  41. if (number == 'f')
  42. return 15;
  43. return -1;
  44. }
  45.  
  46. std::unordered_map<char, int> table{
  47. { '0', 0 },{ '1', 1 },{ '2', 2 },
  48. { '3', 3 },{ '4', 4 },{ '5', 5 },
  49. { '6', 6 },{ '7', 7 },{ '8', 8 },
  50. { '9', 9 },{ 'a', 10 },{ 'A', 10 },
  51. { 'b', 11 },{ 'B', 11 },{ 'c', 12 },
  52. { 'C', 12 },{ 'd', 13 },{ 'D', 13 },
  53. { 'e', 14 },{ 'E', 14 },{ 'f', 15 },
  54. { 'F', 15 },{ 'x', 0 },{ 'X', 0 } };
  55.  
  56. int hextoint2 (char number)
  57. {
  58. return table[(std::size_t)number];
  59. }
  60.  
  61.  
  62. int hextoint4(char number)
  63. {
  64. if (number >= '0' && number <= '9')
  65. return number - '0';
  66. else if (number >= 'a' && number <= 'f')
  67. return number - 'a' + 0x0a;
  68. else return -1;
  69. }
  70.  
  71. struct Mytable {
  72. int x[128];
  73. Mytable() : x{}
  74. {
  75. fill(begin(x), end(x), -1);
  76. for (int i = '0'; i <= '9'; i++)
  77. x[i] = i - '0';
  78. for (int i = 'A'; i <= 'F'; i++)
  79. x[i] = x[i-'A'+'a'] = i - 'A'+0xa;
  80. }
  81. };
  82.  
  83. int hextoint5(char number)
  84. {
  85. static Mytable mytable;
  86. return (number < 0 || number>128) ? -1 : mytable.x[number];
  87. }
  88.  
  89. int hextoint6(char number)
  90. {
  91. switch (number) {
  92. case '0': return 0;
  93. case '1': return 1;
  94. case '2': return 2;
  95. case '3': return 3;
  96. case '4': return 4;
  97. case '5': return 5;
  98. case '6': return 6;
  99. case '7': return 7;
  100. case '8': return 8;
  101. case '9': return 9;
  102. case 'A': case 'a': return 0xa;
  103. case 'B': case 'b': return 0xb;
  104. case 'C': case 'c': return 0xc;
  105. case 'D': case 'd': return 0xd;
  106. case 'E': case 'e': return 0xe;
  107. case 'F': case 'f': return 0xf;
  108. default: return - 1;
  109. }
  110. }
  111.  
  112. const int variants = 5;
  113. const long long maxloop = 10000000;
  114. struct Bench {
  115. int(*f)(char);
  116. char *s;
  117. } bench[variants] = {
  118. hextoint1, "If chain ",
  119. hextoint2, "Map(no error processing)",
  120. hextoint4, "Compact if ",
  121. hextoint5, "Table based ",
  122. hextoint6, "Switch ",
  123. };
  124.  
  125. int main()
  126. {
  127. mt19937 generator;
  128. std::uniform_int_distribution<int> myrandom(0, 16);
  129. const char *s = "0123456789abcdef";
  130. volatile int x;
  131.  
  132. for (int i = 0; i < variants; i++) {
  133. cout << bench[i].s<<" ";
  134. chrono::high_resolution_clock::time_point t1 = chrono::high_resolution_clock::now();
  135. for (long long j = 0; j < maxloop; j++) {
  136. char c = s[myrandom(generator)];
  137. x = bench[i].f(c);
  138. }
  139. chrono::high_resolution_clock::time_point t2 = chrono::high_resolution_clock::now();
  140. long dt = chrono::duration_cast<chrono::milliseconds>(t2 - t1).count();
  141. cout << dt << " ms for " << maxloop << " iterations = " << dt * 1000000 / maxloop << " ns/it" << endl;
  142. }
  143. cin.get();
  144. }
Success #stdin #stdout 1.61s 3464KB
stdin
Standard input is empty
stdout
If chain                 353 ms for 10000000 iterations = 35 ns/it
Map(no error processing) 477 ms for 10000000 iterations = 47 ns/it
Compact if               284 ms for 10000000 iterations = 28 ns/it
Table based              241 ms for 10000000 iterations = 24 ns/it
Switch                   264 ms for 10000000 iterations = 26 ns/it