fork download
  1. #include <iostream>
  2. #include <chrono>
  3. #include <type_traits>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. using namespace std;
  8. using namespace std::chrono;
  9.  
  10. struct Test
  11. {
  12. Test(int a, int b, int c) : a(a), b(b), c(c) { }
  13. int a, b, c;
  14. };
  15.  
  16. const int NUMBER = 100000000;
  17.  
  18. int main()
  19. {
  20. cout << "Creating " << NUMBER << " objects" << endl;
  21.  
  22. high_resolution_clock::time_point start, end;
  23. start = high_resolution_clock::now();
  24.  
  25. for(int i = 0; i < NUMBER; ++i)
  26. {
  27. Test *t = new Test(1, 2, 3);
  28. /* ... */
  29. delete t;
  30. }
  31.  
  32. end = high_resolution_clock::now();
  33. auto duration = duration_cast<milliseconds>(end - start).count();
  34. cout << "Normal: " << duration << "ms" << endl;
  35.  
  36.  
  37. start = high_resolution_clock::now();
  38.  
  39. std::aligned_storage<sizeof(Test), alignof(Test)>::type als[1];
  40. for(int i = 0; i < NUMBER; ++i)
  41. {
  42. Test *t = new(als) Test(1, 2, 3);
  43. /* ... */
  44. t->~Test();
  45. }
  46.  
  47. end = high_resolution_clock::now();
  48. duration = duration_cast<milliseconds>(end - start).count();
  49. cout << "Placement: " << duration << "ms" << endl;
  50.  
  51. return 0;
  52. }
Success #stdin #stdout 1.74s 4368KB
stdin
Standard input is empty
stdout
Creating 100000000 objects
Normal: 1737ms
Placement: 0ms