/*
img1 57. ctr=2048 cpy=4196352 assign=0 loop=0
img2 61. ctr=0 cpy=4194304 assign=0 loop=4194304
img3 46. ctr=4194304 cpy=0 assign=0 loop=2048
img4 24. ctr=4194304 cpy=0 assign=0 loop=0
img1[y][x]= 12. ctr=0 cpy=0 assign=4194304 loop=4194304
img3[y][x]= 12. ctr=0 cpy=0 assign=4194304 loop=4194304
img4[y*n+x]= 15. ctr=0 cpy=0 assign=4194304 loop=4194304
img1 79. ctr=2048 cpy=4196352 assign=0 loop=0
img2 61. ctr=0 cpy=4194304 assign=0 loop=4194304
img3 45. ctr=4194304 cpy=0 assign=0 loop=2048
img4 24. ctr=4194304 cpy=0 assign=0 loop=0
img1[y][x]= 11. ctr=0 cpy=0 assign=4194304 loop=4194304
img3[y][x]= 12. ctr=0 cpy=0 assign=4194304 loop=4194304
img4[y*n+x]= 14. ctr=0 cpy=0 assign=4194304 loop=4194304
*/
#include <iostream>
#include <time.h>
#include <vector>
using namespace std;
static long long ctr=0;
static long long cpy=0;
static long long assign=0;
struct Pixel {
int x,y,z;
Pixel():x(0),y(0),z(0){
ctr++;
}
Pixel(const Pixel& p) {
x=p.x,y=p.y,z=p.z;
cpy++;
}
Pixel& operator=(const Pixel& p) {
x=p.x,y=p.y,z=p.z;
assign++;
return *this;
}
};
int main()
{
int n = 2048;
Pixel c;
clock_t t;
for (int i = 0; i < 2; ++i)
{
// (1) vector<vector>(n,vector<>(n))
ctr = cpy = assign = 0;
t = clock();
vector<vector<Pixel>> img1(n, vector<Pixel>(n));
cout << "img1 " << clock()-t << ". ";
cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\n";
// (2) reserve then push_back
ctr = cpy = assign = 0;
t = clock();
vector<vector<Pixel>> img2(n);
for (int y = 0; y < n; ++y)
img2[y].reserve(n);
for (int y=0; y<n; ++y)
for (int x=0; x<n; ++x)
img2[y].push_back(c);
cout << "img2 " << clock()-t << ". ";
cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << n*n << "\n";
// (3) build 2D array by pointer array
ctr = cpy = assign = 0;
t = clock();
Pixel** img3 = new Pixel*[n];
for (int y = 0; y < n; ++y) img3[y] = new Pixel[n];
cout << "img3 " << clock()-t << ". ";
cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << n << "\n";
// (4) 1D array
ctr = cpy = assign = 0;
t = clock();
Pixel* img4 = new Pixel[n*n];
cout << "img4 " << clock()-t << ". ";
cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\n";
// vector<vector<>> [y][x]
ctr = cpy = assign = 0;
t = clock();
for (int y=0; y<n; ++y)
for (int x=0; x<n; ++x)
img1[y][x] = c;
cout << "img1[y][x]= " << clock()-t << ". ";
cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << n*n << "\n";
// Pixel** by [y][x]
ctr = cpy = assign = 0;
t = clock();
for (int y=0; y<n; ++y)
for (int x=0; x<n; ++x)
img3[y][x] = c;
cout << "img3[y][x]= " << clock()-t << ". ";
cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << n*n << "\n";
// Pixel * by [y*w+x]
ctr = cpy = assign = 0;
t = clock();
for (int y=0; y<n; ++y)
for (int x=0; x<n; ++x)
img4[y*n+x] = c;
cout << "img4[y*n+x]= " << clock()-t << ". ";
cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << n*n << "\n";
for (int y = 0; y < n; ++y) delete [] img3[y];
delete [] img3;
delete [] img4;
}
system("pause");
return 1;
}