fork(1) download
  1. #include <iostream>
  2. #include <future>
  3. #include <chrono>
  4. #include <memory>
  5.  
  6. using namespace std;
  7.  
  8. using std::cout;
  9. using std::cin;
  10. using std::endl;
  11.  
  12. int bar() { return 42; }
  13.  
  14. int main()
  15. {
  16. std::packaged_task<int()> ignore(bar);
  17.  
  18. auto s2 = std::chrono::steady_clock::now();
  19. auto sh_ptr1 = std::make_shared<std::packaged_task<int()> >(bar);
  20. auto e2 = std::chrono::steady_clock::now();
  21.  
  22. auto s1 = std::chrono::steady_clock::now();
  23. std::packaged_task<int()> pt1(bar);
  24. auto e1 = std::chrono::steady_clock::now();
  25.  
  26. auto first = std::chrono::duration_cast<std::chrono::nanoseconds>(e1-s1);
  27. auto second = std::chrono::duration_cast<std::chrono::nanoseconds>(e2-s2);
  28.  
  29. cout << "Regular: " << first.count() << endl
  30. << "Make shared: " << second.count() << endl;
  31.  
  32. return 0;
  33. }
  34.  
Success #stdin #stdout 0s 3284KB
stdin
Standard input is empty
stdout
Regular: 505
Make shared: 937