fork(1) download
  1. #include <iostream>
  2. #include <ctime>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. struct A {
  8. virtual void foo() {}
  9. };
  10.  
  11. struct B : public A {
  12. virtual void foo() override {}
  13. };
  14.  
  15. struct C : public B {
  16. virtual void foo() override {}
  17. };
  18.  
  19. int main()
  20. {
  21. vector<A *> vec;
  22. for (int i = 0; i < 10000000; ++i)
  23. if (i % 2)
  24. vec.push_back(new C());
  25. else
  26. vec.push_back(new B());
  27.  
  28. clock_t begin = clock();
  29. for (auto iter : vec)
  30. {
  31. if (C* cptr = dynamic_cast<C*>(iter))
  32. {
  33. cptr->foo();
  34. }
  35. if (B* bptr = dynamic_cast<B*>(iter))
  36. {
  37. bptr->foo();
  38. }
  39. }
  40. clock_t end = clock();
  41. cout << (static_cast<double>(end) - begin) / CLOCKS_PER_SEC << endl;
  42.  
  43. begin = clock();
  44. for (auto iter : vec)
  45. iter->foo();
  46. end = clock();
  47.  
  48. cout << (static_cast<double>(end) - begin) / CLOCKS_PER_SEC << endl;
  49.  
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0.58s 328384KB
stdin
Standard input is empty
stdout
0.229774
0.028346