code_junkie

Measuring exception handling overhead in C++

Nov 10th, 2011
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. g++ exception_handling.cpp -o exception_handling [ -O2 ]
  2.  
  3. g++ exception_handling.cpp -o exception_handling [ -O2 ] -fno-exceptions -DNO_EXCEPTIONS
  4.  
  5. #include <stdio.h>
  6.  
  7. // Timer code
  8.  
  9. #if defined(__linux__)
  10. #include <sys/time.h>
  11. #include <time.h>
  12.  
  13. double time()
  14. {
  15. timeval tv;
  16. gettimeofday(&tv, 0);
  17. return 1.0 * tv.tv_sec + 0.000001 * tv.tv_usec;
  18. }
  19. #elif defined(_WIN32)
  20. #include <windows.h>
  21.  
  22. double get_performance_frequency()
  23. {
  24. unsigned _int64 frequency;
  25. QueryPerformanceFrequency((LARGE_INTEGER*) &frequency); // just assume it works
  26. return double(frequency);
  27. }
  28.  
  29. double performance_frequency = get_performance_frequency();
  30.  
  31. double time()
  32. {
  33. unsigned _int64 counter;
  34. QueryPerformanceCounter((LARGE_INTEGER*) &counter);
  35. return double(counter) / performance_frequency;
  36. }
  37. #else
  38. # error time() not implemented for your platform
  39. #endif
  40.  
  41. // How many times to repeat the whole test
  42. const int repeats = 10;
  43.  
  44. // How many times to iterate one case
  45. const int times = 1000000;
  46.  
  47. // Trick optimizer to not remove code
  48. int result = 0;
  49.  
  50.  
  51.  
  52. // Case 1. No exception thrown nor handled.
  53.  
  54. void do_something()
  55. {
  56. ++result;
  57. }
  58.  
  59. void case1()
  60. {
  61. do_something();
  62. }
  63.  
  64.  
  65.  
  66. // Case 2. No exception thrown, but handler installed
  67.  
  68. #ifndef NO_EXCEPTIONS
  69. void do_something_else()
  70. {
  71. --result;
  72. }
  73.  
  74. void case2()
  75. {
  76. try
  77. {
  78. do_something();
  79. }
  80. catch (int exception)
  81. {
  82. do_something_else();
  83. }
  84. }
  85.  
  86.  
  87.  
  88. // Case 3. Exception thrown and caught
  89.  
  90. void do_something_and_throw()
  91. {
  92. throw ++result;
  93. }
  94.  
  95. void case3()
  96. {
  97. try
  98. {
  99. do_something_and_throw();
  100. }
  101. catch (int exception)
  102. {
  103. result = exception;
  104. }
  105. }
  106. #endif // !NO_EXCEPTIONS
  107.  
  108. void (*tests[])() =
  109. {
  110. case1,
  111. #ifndef NO_EXCEPTIONS
  112. case2,
  113. case3
  114. #endif // !NO_EXCEPTIONS
  115. };
  116.  
  117. int main()
  118. {
  119. #ifdef NO_EXCEPTIONS
  120. printf("case0n");
  121. #else
  122. printf("case1tcase2tcase3n");
  123. #endif
  124. for (int repeat = 0; repeat < repeats; ++repeat)
  125. {
  126. for (int test = 0; test < sizeof(tests)/sizeof(tests[0]); ++test)
  127. {
  128. double start = time();
  129.  
  130. for (int i = 0; i < times; ++i)
  131. tests[test]();
  132.  
  133. double end = time();
  134.  
  135. printf("%ft", (end - start) * 1000000.0 / times);
  136. }
  137. printf("n");
  138. }
  139.  
  140. return result; // optimizer is happy - we produce a result
  141. }
Add Comment
Please, Sign In to add comment