1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
   | #include <vector>
#include <chrono>
#include <iostream>
 
using collec_type = std::vector<int>;
 
class A {
  collec_type v_;
 public:
  A() : v_ {1,2,4,4,5,6,7,8,9} {}
};
 
class B {
  collec_type v_;
 public:
  B() { v_ = collec_type({1,2,4,4,5,6,7,8,9}); }
};
 
int main() {
  using namespace std::chrono;
  constexpr size_t nb_iter = 1e7; 
 
  auto t1 = high_resolution_clock::now();
  for(size_t i = 0; i < nb_iter; ++i)
    A a;
  auto t2 = high_resolution_clock::now();
  for(size_t i = 0; i < nb_iter; ++i) 
    B b;
  auto t3 = high_resolution_clock::now();
 
  std::cout << "Init list: " << duration_cast<milliseconds>(t2-t1).count() << "ms\n";
  std::cout << "Ctor body: " << duration_cast<milliseconds>(t3-t2).count() << "ms\n";
 
} | 
Partager