#include <cstdlib>
#include <vector>
#include <iostream>
#include <string>
#include <chrono>
class TestTimer
{
public:
using clock = std::chrono::high_resolution_clock;
using time_point = clock::time_point;
using duration = clock::duration;
TestTimer(const std::string & name) : name(name),
start(clock::now())
{
}
~TestTimer()
{
time_point now(clock::now());
duration d = now - start;
std::cout << name << " completed in " << d.count() * (double)duration::period::num / duration::period::den <<
" seconds" << std::endl;
}
private:
std::string name;
time_point start;
};
struct Pixel
{
Pixel()
{
}
Pixel(unsigned char r, unsigned char g, unsigned char b) : r(r), g(g), b(b)
{
}
unsigned char r, g, b;
};
void UseVector()
{
TestTimer t("UseVector");
for(int i = 0; i < 1000; ++i)
{
int dimension = 999;
std::vector<Pixel> pixels;
pixels.resize(dimension * dimension);
for(int i = 0; i < dimension * dimension; ++i)
{
pixels[i].r = 255;
pixels[i].g = 0;
pixels[i].b = 0;
}
}
}
void UseVectorPushBack()
{
TestTimer t("UseVectorPushBack");
for(int i = 0; i < 1000; ++i)
{
int dimension = 999;
std::vector<Pixel> pixels;
pixels.reserve(dimension * dimension);
for(int i = 0; i < dimension * dimension; ++i)
pixels.push_back(Pixel(255, 0, 0));
}
}
void UseArray()
{
TestTimer t("UseArray");
for(int i = 0; i < 1000; ++i)
{
int dimension = 999;
Pixel * pixels = (Pixel *)malloc(sizeof(Pixel) * dimension * dimension);
for(int i = 0 ; i < dimension * dimension; ++i)
{
pixels[i].r = 255;
pixels[i].g = 0;
pixels[i].b = 0;
}
free(pixels);
}
}
int main()
{
TestTimer t1("The whole thing");
UseArray();
//UseVector();
//UseVectorPushBack();
return 0;
}
I2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDx2ZWN0b3I+CgojaW5jbHVkZSA8aW9zdHJlYW0+CiNpbmNsdWRlIDxzdHJpbmc+CgojaW5jbHVkZSA8Y2hyb25vPgoKY2xhc3MgVGVzdFRpbWVyCnsKcHVibGljOgogICAgdXNpbmcgY2xvY2sgPSBzdGQ6OmNocm9ubzo6aGlnaF9yZX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gcGl4ZWxzLnJlc2l6ZShkaW1lbnNpb24gKiBkaW1lbnNpb24pOwoKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgZGltZW5zaW9uICogZGltZW5zaW9uOyArK2kpCiAgICAgICAgewogICAgICAgICAgICBwaXhlbHNbaV0uciA9IDI1NTsKICAgICAgICAgICAgcGl4ZWxzW2ldLmcgPSAwOwogICAgICAgICAgICBwaXhlbHNbaV0uYiA9IDA7CiAgICAgICAgfQogICAgfQp9Cgp2b2lkIFVzZVZlY3RvclB1c2hCYWNrKCkKewogICAgVGVzdFRpbWVyIHQoIlVzZVZlY3RvclB1c2hCYWNrIik7CgogICAgZm9yKGludCBpID0gMDsgaSA8IDEwMDA7ICsraSkKICAgIHsKICAgICAgICBpbnQgZGltZW5zaW9uID0gOTk5OwoKICAgICAgICBzdGQ6OnZlY3RvcjxQaXhlbD4gcGl4ZWxzOwogICAgICAgICAgICBwaXhlbHMucmVzZXJ2ZShkaW1lbnNpb24gKiBkaW1lbnNpb24pOwoKICAgICAgICBmb3IoaW50IGkgPSAwOyBpIDwgZGltZW5zaW9uICogZGltZW5zaW9uOyArK2kpCiAgICAgICAgICAgIHBpeGVscy5wdXNoX2JhY2soUGl4ZWwoMjU1LCAwLCAwKSk7CiAgICB9Cn0KCnZvaWQgVXNlQXJyYXkoKQp7CiAgICBUZXN0VGltZXIgdCgiVXNlQXJyYXkiKTsKCiAgICBmb3IoaW50IGkgPSAwOyBpIDwgMTAwMDsgKytpKQogICAgewogICAgICAgIGludCBkaW1lbnNpb24gPSA5OTk7CgogICAgICAgIFBpeGVsICogcGl4ZWxzID0gKFBpeGVsICopbWFsbG9jKHNpemVvZihQaXhlbCkgKiBkaW1lbnNpb24gKiBkaW1lbnNpb24pOwoKICAgICAgICBmb3IoaW50IGkgPSAwIDsgaSA8IGRpbWVuc2lvbiAqIGRpbWVuc2lvbjsgKytpKQogICAgICAgIHsKICAgICAgICAgICAgcGl4ZWxzW2ldLnIgPSAyNTU7CiAg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