#include <iostream>
#include <chrono>
#include <type_traits>
#include <stdlib.h>
#include <time.h>
using namespace std;
using namespace std::chrono;
struct Test
{
Test(int a, int b, int c) : a(a), b(b), c(c) { }
int a, b, c;
};
const int NUMBER = 100000000;
int main()
{
cout << "Creating " << NUMBER << " objects" << endl;
high_resolution_clock::time_point start, end;
start = high_resolution_clock::now();
for(int i = 0; i < NUMBER; ++i)
{
Test *t = new Test(1, 2, 3);
/* ... */
delete t;
}
end = high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(end - start).count();
cout << "Normal: " << duration << "ms" << endl;
start = high_resolution_clock::now();
std::aligned_storage<sizeof(Test), alignof(Test)>::type als[1];
for(int i = 0; i < NUMBER; ++i)
{
Test *t = new(als) Test(1, 2, 3);
/* ... */
t->~Test();
}
end = high_resolution_clock::now();
duration = duration_cast<milliseconds>(end - start).count();
cout << "Placement: " << duration << "ms" << endl;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y2hyb25vPgojaW5jbHVkZSA8dHlwZV90cmFpdHM+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHRpbWUuaD4gCgp1c2luZyBuYW1lc3BhY2Ugc3RkOwp1c2luZyBuYW1lc3BhY2Ugc3RkOjpjaHJvbm87CgpzdHJ1Y3QgVGVzdAp7CiAgVGVzdChpbnQgYSwgaW50IGIsIGludCBjKSA6IGEoYSksIGIoYiksIGMoYykgeyB9CiAgaW50IGEsIGIsIGM7Cn07Cgpjb25zdCBpbnQgTlVNQkVSID0gMTAwMDAwMDAwOwoKaW50IG1haW4oKQp7CiAgY291dCA8PCAiQ3JlYXRpbmcgIiA8PCBOVU1CRVIgPDwgIiBvYmplY3RzIiA8PCBlbmRsOwoKICBoaWdoX3Jlc29sdXRpb25fY2xvY2s6OnRpbWVfcG9pbnQgc3RhcnQsIGVuZDsKICBzdGFydCA9IGhpZ2hfcmVzb2x1dGlvbl9jbG9jazo6bm93KCk7CiAgCiAgZm9y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