fork(4) download
  1. #include <iostream>
  2. #include <stack>
  3. #include <ctime>
  4. #include <vector>
  5.  
  6. std::stack<clock_t> tictoc_stack;
  7.  
  8. void tic() {
  9. tictoc_stack.push(clock());
  10. }
  11.  
  12. void toc() {
  13. std::cout << "Time elapsed: "
  14. << ((double)(clock() - tictoc_stack.top())) / CLOCKS_PER_SEC
  15. << std::endl;
  16. tictoc_stack.pop();
  17. }
  18.  
  19. using namespace std;
  20.  
  21. int main() {
  22.  
  23. int sizes[] = {5, 10, 20, 30, 40, 50, 100, 200, 300, 400, 500, 1000};
  24.  
  25. for (int k = 0; k < 12; ++k) {
  26.  
  27. int SIZE = sizes[k];
  28.  
  29. cout << "SIZE " << SIZE << endl;
  30.  
  31. vector<vector<int> > v(SIZE, std::vector<int>(SIZE));
  32.  
  33. tic();
  34.  
  35. for (int t = 0; t < 1000; ++t) {
  36.  
  37. for (int i = 0; i < SIZE; ++i) {
  38. for (int j = 0; j < SIZE; ++j) {
  39. v[i][j] = v[i][j] + 5;
  40. }
  41. }
  42.  
  43. }
  44.  
  45. toc();
  46.  
  47. vector<int > v2(SIZE*SIZE);
  48.  
  49.  
  50. tic();
  51.  
  52. for (int t = 0; t < 1000; ++t) {
  53.  
  54. for (int i = 0; i < SIZE*SIZE; ++i) {
  55.  
  56. v2[i] = v2[i] + 5;
  57.  
  58. }
  59.  
  60. }
  61.  
  62. toc();
  63.  
  64.  
  65. }
  66. }
Success #stdin #stdout 3.67s 3476KB
stdin
Standard input is empty
stdout
SIZE 5
Time elapsed: 4.5e-05
Time elapsed: 1.2e-05
SIZE 10
Time elapsed: 7.2e-05
Time elapsed: 3.3e-05
SIZE 20
Time elapsed: 0.000226
Time elapsed: 0.000128
SIZE 30
Time elapsed: 0.000424
Time elapsed: 0.000257
SIZE 40
Time elapsed: 0.000604
Time elapsed: 0.000475
SIZE 50
Time elapsed: 0.000893
Time elapsed: 0.000714
SIZE 100
Time elapsed: 0.003429
Time elapsed: 0.002941
SIZE 200
Time elapsed: 0.021113
Time elapsed: 0.021656
SIZE 300
Time elapsed: 0.046727
Time elapsed: 0.048937
SIZE 400
Time elapsed: 0.09614
Time elapsed: 0.097717
SIZE 500
Time elapsed: 0.158564
Time elapsed: 0.163708
SIZE 1000
Time elapsed: 1.47827
Time elapsed: 1.50852