fork 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 = shvec->y;
  28. shvec->z = work;
  29. }
  30. time = std::chrono::high_resolution_clock::now();
  31. std::cout << " shared pointer: " << (time - lastTime).count() << std::endl;
  32.  
  33.  
  34. lastTime = std::chrono::high_resolution_clock::now();
  35. for( int i=0; i<count; ++i )
  36. {
  37. work = vec->y;
  38. vec->z = work;
  39. }
  40. time = std::chrono::high_resolution_clock::now();
  41. std::cout << "standard pointer: " << (time - lastTime).count() << std::endl;
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
  shared pointer: 255
standard pointer: 38