fork(2) download
  1. #include <ctime>
  2. #include <iostream>
  3. #include <stdlib.h> /* srand, rand */
  4.  
  5. using namespace std;
  6.  
  7. class C{
  8. public: int data=0;
  9. };
  10. class B{
  11. public: C* c; int accu=0;
  12. public: B(){
  13. c=new C();
  14. }
  15. public: void doSomething(){
  16. accu+=c->data; //do something about c
  17. }
  18. };
  19. int main() {
  20. using namespace std;
  21.  
  22. const int NUM=1000000;
  23. B* bs[NUM];
  24. for(int n=0;n<NUM;n++){
  25. bs[n]=new B();
  26. }
  27. for(int loop=0;loop<20;loop++){
  28. double accumulator=0;
  29.  
  30. for(int n=0;n<NUM;n++){
  31. int iSecret = rand() % NUM;
  32. clock_t begin = clock();
  33. bs[iSecret]->doSomething();
  34. clock_t end = clock();
  35. accumulator+=double(end - begin);
  36. }
  37.  
  38. double elapsed_secs = accumulator;
  39. std::cout<<elapsed_secs<<std::endl;
  40. }
  41. }
Time limit exceeded #stdin #stdout 5s 148672KB
stdin
Standard input is empty
stdout
299587
299366
299982
303101
301583
301165
297351
297098
299671
302878
298860