fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <memory>
  5.  
  6. struct base {
  7. virtual ~base(){}
  8. virtual int foo()=0;
  9. };
  10. struct derived_1 : base {
  11. derived_1() : data(1){}
  12. int data;
  13. virtual int foo(){return data++;}
  14. };
  15. struct derived_2 : base {
  16. derived_2() : data1(1), data2(1) {}
  17. int data1;
  18. int data2;
  19. virtual int foo(){return --data1+ ++data2;}
  20. };
  21.  
  22. int main()
  23. {
  24. using namespace std;
  25. const int howmany = 10*1000;
  26. const int howoften = 10*1000;
  27.  
  28. vector<unique_ptr<base> > foo;
  29.  
  30. for (int i=0; i<howmany; ++i) {
  31. foo.push_back(unique_ptr<base>(new derived_1()));
  32. foo.push_back(unique_ptr<base>(new derived_2()));
  33. }
  34.  
  35. int sum = 0;
  36. for (int i=0; i<howoften; ++i) {
  37. for (auto& b : foo) {
  38. sum += b->foo();
  39. }
  40. }
  41. std::cout << sum << '\n';
  42. }
  43.  
  44.  
Success #stdin #stdout 2.44s 3252KB
stdin
Standard input is empty
stdout
2033793664