/*
VC2010, release, optimization /ox
output of disable counter
img1 50
img2 57
img3 45
img4 16
img1[y][x]= 7
img3[y][x]= 6
img4[y*n+x]= 10
img1 75
img2 63
img3 49
img4 16
img1[y][x]= 8
img3[y][x]= 7
img4[y*n+x]= 10.
*/
#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 << "\n";
//cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\n\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 << "\n";
//cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\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 << "\n";
//cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\n\n";
// (4) 1D array
ctr = cpy = assign = 0;
t = clock();
Pixel* img4 = new Pixel[n*n];
cout << "img4 " << clock()-t << "\n";
//cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\n\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 << "\n";
//cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\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 << "\n";
//cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\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 << "\n";
//cout << "ctr=" << ctr << " cpy=" << cpy << " assign=" << assign << " loop=" << 0 << "\n\n";
for (int y = 0; y < n; ++y) delete [] img3[y];
delete [] img3;
delete [] img4;
}
system("pause");
return 1;
}