fork download
  1. /*
  2. VC2010, release, optimization /ox
  3. output of disable counter
  4. img1 50
  5. img2 57
  6. img3 45
  7. img4 16
  8. img1[y][x]= 7
  9. img3[y][x]= 6
  10. img4[y*n+x]= 10
  11. img1 75
  12. img2 63
  13. img3 49
  14. img4 16
  15. img1[y][x]= 8
  16. img3[y][x]= 7
  17. img4[y*n+x]= 10.
  18. */
  19. #include <iostream>
  20. #include <time.h>
  21. #include <vector>
  22. using namespace std;
  23. static long long ctr=0;
  24. static long long cpy=0;
  25. static long long assign=0;
  26. struct Pixel {
  27. int x,y,z;
  28. Pixel():x(0),y(0),z(0){
  29. //ctr++;
  30. }
  31. //Pixel(const Pixel& p) {
  32. // x=p.x,y=p.y,z=p.z;
  33. // cpy++;
  34. //}
  35. //Pixel& operator=(const Pixel& p) {
  36. // x=p.x,y=p.y,z=p.z;
  37. // assign++;
  38. // return *this;
  39. //}
  40. };
  41. int main()
  42. {
  43. int n = 2048;
  44. Pixel c;
  45. clock_t t;
  46. for (int i = 0; i < 2; ++i)
  47. {
  48. // (1) vector<vector>(n,vector<>(n))
  49. ctr = cpy = assign = 0;
  50. t = clock();
  51. vector<vector<Pixel>> img1(n, vector<Pixel>(n));
  52. cout << "img1 " << clock()-t << "\n";
  53. //cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\n\n";
  54.  
  55. // (2) reserve then push_back
  56. ctr = cpy = assign = 0;
  57. t = clock();
  58. vector<vector<Pixel>> img2(n);
  59. for (int y = 0; y < n; ++y)
  60. img2[y].reserve(n);
  61. for (int y=0; y<n; ++y)
  62. for (int x=0; x<n; ++x)
  63. img2[y].push_back(c);
  64. cout << "img2 " << clock()-t << "\n";
  65. //cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\n\n";
  66.  
  67. // (3) build 2D array by pointer array
  68. ctr = cpy = assign = 0;
  69. t = clock();
  70. Pixel** img3 = new Pixel*[n];
  71. for (int y = 0; y < n; ++y) img3[y] = new Pixel[n];
  72. cout << "img3 " << clock()-t << "\n";
  73. //cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\n\n";
  74.  
  75. // (4) 1D array
  76. ctr = cpy = assign = 0;
  77. t = clock();
  78. Pixel* img4 = new Pixel[n*n];
  79. cout << "img4 " << clock()-t << "\n";
  80. //cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\n\n";
  81.  
  82. // vector<vector<>> [y][x]
  83. ctr = cpy = assign = 0;
  84. t = clock();
  85. for (int y=0; y<n; ++y)
  86. for (int x=0; x<n; ++x)
  87. img1[y][x] = c;
  88. cout << "img1[y][x]= " << clock()-t << "\n";
  89. //cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\n\n";
  90.  
  91. // Pixel** by [y][x]
  92. ctr = cpy = assign = 0;
  93. t = clock();
  94. for (int y=0; y<n; ++y)
  95. for (int x=0; x<n; ++x)
  96. img3[y][x] = c;
  97. cout << "img3[y][x]= " << clock()-t << "\n";
  98. //cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\n\n";
  99.  
  100. // Pixel * by [y*w+x]
  101. ctr = cpy = assign = 0;
  102. t = clock();
  103. for (int y=0; y<n; ++y)
  104. for (int x=0; x<n; ++x)
  105. img4[y*n+x] = c;
  106. cout << "img4[y*n+x]= " << clock()-t << "\n";
  107. //cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\n\n";
  108.  
  109.  
  110. for (int y = 0; y < n; ++y) delete [] img3[y];
  111. delete [] img3;
  112. delete [] img4;
  113.  
  114. }
  115. system("pause");
  116. return 1;
  117. }
Runtime error #stdin #stdout #stderr 0.49s 3476KB
stdin
Standard input is empty
stdout
img1 40000
img2 50000
img3 50000
img4 30000
img1[y][x]= 20000
img3[y][x]= 20000
img4[y*n+x]= 20000
img1 50000
img2 50000
img3 40000
img4 40000
img1[y][x]= 20000
img3[y][x]= 20000
img4[y*n+x]= 20000
stderr
sh: pause: not found