fork(9) download
  1. #include <iostream>
  2. #include <memory>
  3. #include <thread>
  4. #include <chrono>
  5.  
  6.  
  7. struct Vec
  8. {
  9. double x, y, z;
  10. };
  11.  
  12.  
  13. int main()
  14. {
  15. Vec* vec = new Vec{ 0,0,0 };
  16. std::shared_ptr<Vec> shvec{ new Vec{0,0,0} };
  17.  
  18. std::this_thread::sleep_for(std::chrono::milliseconds{1000});
  19.  
  20. double work;
  21. const int count = 10000;
  22. std::chrono::high_resolution_clock::time_point time, lastTime;
  23.  
  24. lastTime = std::chrono::high_resolution_clock::now();
  25. for( int i=0; i<count; ++i )
  26. {
  27. work = vec->y;
  28. vec->z = work;
  29. }
  30. time = std::chrono::high_resolution_clock::now();
  31. std::cout << "standard pointer: " << (time - lastTime).count() << std::endl;
  32.  
  33. lastTime = std::chrono::high_resolution_clock::now();
  34. for( int i=0; i<count; ++i )
  35. {
  36. work = shvec->y;
  37. shvec->z = work;
  38. }
  39. time = std::chrono::high_resolution_clock::now();
  40. std::cout << " shared pointer: " << (time - lastTime).count() << std::endl;
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
standard pointer: 920
  shared pointer: 573