fork download
  1. #include <string>
  2. #include <iostream>
  3. #include <system_error>
  4. #include <cerrno>
  5. #include <signal.h>
  6.  
  7. #include <cstdint>
  8.  
  9. uint64_t rdtsc();
  10.  
  11. #if defined(__i386) || defined(__x86_64__) // gcc,clang
  12. inline uint64_t rdtsc()
  13. {
  14. uint32_t tickl, tickh;
  15. asm volatile("rdtsc":"=a"(tickl),"=d"(tickh));
  16. return ( static_cast<uint64_t>(tickh) << 32) | tickl;
  17. }
  18. #elif defined(_M_IX86) || defined(_M_X64) // MSVC
  19. #include <intrin.h>
  20. #pragma intrinsic(__rdtsc)
  21.  
  22. inline uint64_t rdtsc()
  23. {
  24. return __rdtsc();
  25. }
  26. #endif
  27.  
  28.  
  29. template<typename F>
  30. void bench(std::string name,
  31. F func)
  32. {
  33. std::cout<<"Benchmarking \"" << name << "\"\n";
  34. uint64_t tsc_begin, second_run, first_run;
  35. // first run
  36. tsc_begin = rdtsc();
  37. func();
  38. first_run = rdtsc() - tsc_begin;
  39. // second run
  40. tsc_begin = rdtsc();
  41. func();
  42. second_run = rdtsc() - tsc_begin;
  43. std::cout << "tsc: " << first_run << " " << second_run << "\n";
  44. }
  45.  
  46. int failing_func(){
  47. sigset_t sig;
  48. return sigprocmask(-1, &sig, nullptr);
  49. }
  50.  
  51. int success_func(){
  52. return sigprocmask(0, nullptr, nullptr);
  53. }
  54.  
  55. void with_exception_oftenfail(){
  56. int res = failing_func();
  57. if(res == -1) throw std::system_error(errno, std::system_category());
  58. }
  59.  
  60. int with_error_code_oftenfail(){
  61. return failing_func();
  62. }
  63.  
  64. void with_exception_rarefail(){
  65. int res = success_func();
  66. if(res == -1) throw std::system_error(errno, std::system_category());
  67. }
  68.  
  69. int with_error_code_rarefail(){
  70. return success_func();
  71. }
  72.  
  73. int main(){
  74. bench( "rdtsc()", [](){} );
  75.  
  76. unsigned int black_hole = 0;
  77. bench("Exceptions (often error)", [&black_hole](){
  78. try{
  79. with_exception_oftenfail();
  80. } catch(std::exception&){
  81. black_hole++;
  82. }
  83. });
  84.  
  85. bench("Error codes (often error)", [&black_hole](){
  86. int res = with_error_code_oftenfail();
  87. if(res == -1) black_hole--;
  88. });
  89.  
  90. bench("Exceptions (rare error)", [&black_hole](){
  91. try{
  92. with_exception_rarefail();
  93. } catch(std::exception&){
  94. black_hole++;
  95. }
  96. });
  97.  
  98. bench("Error codes (rare error)", [&black_hole](){
  99. int res = with_error_code_rarefail();
  100. if(res == -1) black_hole--;
  101. });
  102.  
  103. //To prevent loop optimize-out
  104. std::cout<<"Blackhole is:"<<black_hole<<std::endl;
  105. }
  106.  
Success #stdin #stdout 0s 16072KB
stdin
Standard input is empty
stdout
Benchmarking "rdtsc()"
tsc: 21 22
Benchmarking "Exceptions (often error)"
tsc: 79415 11341
Benchmarking "Error codes (often error)"
tsc: 208 190
Benchmarking "Exceptions (rare error)"
tsc: 214 139
Benchmarking "Error codes (rare error)"
tsc: 149 122
Blackhole is:0