fork download
  1. #include <boost/range/algorithm/nth_element.hpp>
  2. #include <boost/preprocessor/arithmetic/sub.hpp>
  3. #include <boost/preprocessor/repeat.hpp>
  4. #include <boost/preprocessor/cat.hpp>
  5. #include <boost/noncopyable.hpp>
  6. #include <boost/timer/timer.hpp>
  7. //#include <boost/timer.hpp>
  8.  
  9. #include <iostream>
  10. #include <ostream>
  11. #include <cstdlib>
  12. #include <vector>
  13. #include <ctime>
  14.  
  15. using namespace std;
  16. using namespace boost;
  17.  
  18. // ______________________________ //
  19.  
  20. const int series = 33;
  21. const int deepness = 5;
  22. #define REPEATS 32
  23.  
  24. // ______________________________ //
  25.  
  26. bool error_check()
  27. {
  28. //return time(0) < 1;
  29. static volatile bool t = false;
  30. return t;
  31. }
  32. void *mallocator()
  33. {
  34. return malloc(4);
  35. }
  36.  
  37. // ______________________________ //
  38.  
  39. class Holder : private noncopyable
  40. {
  41. void *data;
  42. public:
  43. Holder()
  44. : data(mallocator())
  45. {}
  46. ~Holder()
  47. {
  48. free(data);
  49. }
  50. };
  51. template<int iter> struct exceptions
  52. {
  53. static void call()
  54. {
  55. #define EXP_REP(z, i, _) Holder h##i; exceptions<iter-1>::call();
  56. BOOST_PP_REPEAT(REPEATS,EXP_REP,t)
  57. }
  58. };
  59. template<> struct exceptions<0>
  60. {
  61. static void call()
  62. {
  63. if(error_check()) throw false;
  64. }
  65. };
  66.  
  67. // ______________________________ //
  68.  
  69. template<int iter> struct ifgoto
  70. {
  71. static bool call()
  72. {
  73. bool result = true;
  74.  
  75. #define IFG_REP0(z, i, _) void *d##i;
  76. BOOST_PP_REPEAT(REPEATS,IFG_REP0,t)
  77.  
  78. #define IFG_REP1(z, i, _) d##i = mallocator(); if(! (result=ifgoto<iter-1>::call()) ) goto ERROR##i;
  79. BOOST_PP_REPEAT(REPEATS,IFG_REP1,t)
  80.  
  81. #define IFG_REP_AUX(i) BOOST_PP_CAT(ERROR,i): free(BOOST_PP_CAT(d,i));
  82. #define IFG_REP2(z, i, _) IFG_REP_AUX( BOOST_PP_SUB(BOOST_PP_SUB(REPEATS,1),i) )
  83. BOOST_PP_REPEAT(REPEATS,IFG_REP2,t)
  84.  
  85. return result;
  86. }
  87. };
  88. template<> struct ifgoto<0>
  89. {
  90. static bool call()
  91. {
  92. return error_check() == false;
  93. }
  94. };
  95.  
  96. // ______________________________ //
  97.  
  98. template<template<int> class test_case>
  99. void test(const char *name)
  100. {
  101. cout << name;
  102. vector<double> times;
  103. for(int i=0;i!=series;++i)
  104. {
  105. //timer t;
  106. timer::cpu_timer t;
  107. test_case<deepness>::call();
  108. times.push_back(double(t.elapsed().user));
  109. }
  110. vector<double>::iterator median = times.begin() + times.size()/2;
  111. nth_element(times,median);
  112. cout << "\tmedian is " << (*median)*1e-6 << "ms" << endl;
  113. }
  114.  
  115. int main()
  116. {
  117. try
  118. {
  119. for(int i=0;i!=2;++i)
  120. {
  121. test<exceptions>("exceptions");
  122. test<ifgoto>("if-error-goto");
  123. }
  124. }
  125. catch(...){}
  126. }
  127.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty