fork(1) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cstdlib>
  4. #include <chrono>
  5.  
  6. using namespace std;
  7.  
  8. struct base
  9. {
  10. int type;
  11. virtual int get() = 0;
  12. virtual ~base() {}
  13. };
  14.  
  15. struct class1 : base
  16. {
  17. class1() { type = 1; }
  18. virtual int get() { return 11; }
  19. };
  20.  
  21. struct class2 : public base
  22. {
  23. class2() { type = 2; }
  24. virtual int get() { return 27; }
  25. };
  26.  
  27. class timer
  28. {
  29. using T = std::chrono::time_point<std::chrono::steady_clock>;
  30. T t1, t2;
  31. public:
  32. void start() { t1 = std::chrono::steady_clock::now(); }
  33. void stop() { t2 = std::chrono::steady_clock::now(); }
  34. void print() const { std::cout << "Time: " << (t2 - t1).count() << endl; }
  35. };
  36.  
  37. int main()
  38. {
  39. vector <base *> v;
  40. int s = 0;
  41. timer t;
  42.  
  43. srand(time(0));
  44. for (unsigned q=0; q<50000000; ++q) v.push_back(rand()&1 ? (base*)new class1() : (base*)new class2());
  45.  
  46. t.start();
  47. for (auto x : v) s += x->get();
  48. t.stop();
  49.  
  50. cout << s << endl;
  51. t.print();
  52.  
  53. for (auto x : v) delete x;
  54.  
  55. return 0;
  56. }
Success #stdin #stdout 3.21s 1577472KB
stdin
Standard input is empty
stdout
949992080
Time: 285762729