fork download
  1. #include <iostream>
  2. #include <list>
  3. #include <boost/foreach.hpp>
  4. #include <time.h>
  5.  
  6.  
  7. struct Base{
  8. int idLev1;
  9. int idLev2;
  10. Base(int a, int b)
  11. {
  12. idLev1 = a;
  13. idLev2 = b;
  14. }
  15. // just for dynamic cast
  16. virtual ~Base(){}
  17. };
  18.  
  19. struct DerivedA : public Base
  20. {
  21. DerivedA(): Base(1,1){}
  22. };
  23.  
  24. struct DerivedB : public Base
  25. {
  26. DerivedB(): Base(2,1){}
  27. };
  28.  
  29. struct DerivedC : public Base
  30. {
  31. DerivedC(): Base(3,1){}
  32. DerivedC(int a, int b): Base(a,b){}
  33. };
  34.  
  35. struct DerivedX : public DerivedC
  36. {
  37. DerivedX(): DerivedC(3,2){}
  38. };
  39.  
  40. struct DerivedY : public DerivedC
  41. {
  42. DerivedY(): DerivedC(3,3){}
  43. };
  44.  
  45.  
  46. template<class T>
  47. void findDerived(const std::list<Base*> &base, int& c, double& itime)
  48. {
  49. double tstart, tend;
  50. tstart = (double)clock()/CLOCKS_PER_SEC;
  51. BOOST_FOREACH(Base* x,base)
  52. {
  53.  
  54. if(dynamic_cast<T>(x)) ++c;
  55.  
  56. }
  57.  
  58. tend = (double)clock()/CLOCKS_PER_SEC;
  59. itime += tend - tstart;
  60.  
  61. }
  62.  
  63. void findNumber(const std::list<Base*> &base, const int a, const int b, int &c, double& itime)
  64. {
  65.  
  66. double tstart, tend;
  67. tstart= (double)clock()/CLOCKS_PER_SEC;
  68. BOOST_FOREACH(Base* x,base)
  69. {
  70. if(x->idLev1 == a && x->idLev2 == b) ++c;
  71. }
  72. tend = (double)clock()/CLOCKS_PER_SEC;
  73. itime += tend - tstart;
  74.  
  75. }
  76. int main() {
  77. std::list <Base*> b;
  78. for (int i = 1; i<1999000; ++i){
  79. if (rand()%2) b.push_back(new DerivedA());
  80. if (rand()%2) b.push_back(new DerivedB());
  81. if (rand()%2) b.push_back(new DerivedC());
  82. if (rand()%2) b.push_back(new DerivedX());
  83. if (rand()%2) b.push_back(new DerivedY());
  84. }
  85.  
  86. int c=0;int d=0;
  87. double itime1=0; double itime2=0;
  88. std::cout << "Method a\n";
  89.  
  90. findDerived<DerivedA*>(b, c, itime1);
  91. findDerived<DerivedB*>(b, c, itime1);
  92. findDerived<DerivedC*>(b, c, itime1);
  93. findDerived<DerivedX*>(b, c, itime1);
  94. findDerived<DerivedY*>(b, c, itime1);
  95. std::cout << "Method a\n";
  96. findNumber<DerivedA*>(b, d, itime2);
  97. findNumber<DerivedB*>(b, d, itime2);
  98. findNumber<DerivedC*>(b, d, itime2);
  99. findNumber<DerivedX*>(b, d, itime2);
  100. findNumber<DerivedY*>(b, d, itime2);
  101. std::cout << "Time method a " << itime1 << "count " << c << std::endl;
  102. std::cout << "Time method b " << itime2 << "count " << d << std::endl;
  103. return 0;
  104. }
Compilation error #stdin compilation error #stdout 3.29s 158912KB
stdin
Standard input is empty
compilation info
prog.cpp:3:29: fatal error: boost/foreach.hpp: No such file or directory
 #include <boost/foreach.hpp>
                             ^
compilation terminated.
stdout
Standard output is empty