Advertisement
Guest User

Untitled

a guest
Dec 4th, 2014
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <memory>
  2. #include <vector>
  3. #include <algorithm>
  4. #include "/opt/cpp/include/utils/sole_ptr.hpp"
  5.  
  6. struct GameObject {
  7.     virtual void foo() {
  8.         // dummy function
  9.     }
  10. };
  11.  
  12. // shared-weak-Ansatz
  13. using SharedGO = std::shared_ptr<GameObject>;
  14. using WeakGO   = std::weak_ptr<GameObject>;
  15.  
  16. // sole-dynamic-Ansatz
  17. using SoleGO    = sole_ptr<GameObject>;
  18. using DynamicGO = dynamic_ptr<GameObject>;
  19.  
  20. // unique-raw-Ansatz
  21. using UniqueGO = std::unique_ptr<GameObject>;
  22. using RawGO    = GameObject*;
  23.  
  24. // ---------------------------------------------------------------------------
  25.  
  26. int const NUM_OBJECTS = 100;
  27. int const NUM_ITERATIONS = 100000;
  28.  
  29. void test_shared() {
  30.     std::vector<SharedGO> dungeon;
  31.     std::vector<WeakGO> ai;
  32.  
  33.     for (int i = 0; i < NUM_OBJECTS; i++) {
  34.         // GameObject created outside (e.g. via factory)
  35.         SharedGO object{new GameObject()};
  36.         // owning ptr spawned to dungeon; non-owning ptr returned
  37.         dungeon.push_back(std::move(object));
  38.         WeakGO weak{dungeon.back()};
  39.         // non-owning ptr stored in AI's list
  40.         ai.push_back(weak);
  41.     }
  42.  
  43.     for (int i = 0; i < NUM_ITERATIONS; i++) {
  44.         for (auto&& weak: ai) {
  45.             // dereference ai-focused object
  46.             GameObject& object = *(weak.lock());
  47.             object.foo();
  48.         }
  49.     }
  50. }
  51.  
  52. void test_sole() {
  53.     std::vector<SoleGO> dungeon;
  54.     std::vector<DynamicGO> ai;
  55.  
  56.     for (int i = 0; i < NUM_OBJECTS; i++) {
  57.         SoleGO object{new GameObject()};
  58.         dungeon.push_back(std::move(object));
  59.         DynamicGO dynamic{dungeon.back()};
  60.         ai.push_back(dynamic);
  61.     }
  62.  
  63.     for (int i = 0; i < NUM_ITERATIONS; i++) {
  64.         for (auto&& dynamic: ai) {
  65.             GameObject& object = *dynamic;
  66.             object.foo();
  67.         }
  68.     }
  69. }
  70.  
  71. void test_unique() {
  72.     std::vector<UniqueGO> dungeon;
  73.     std::vector<RawGO> ai;
  74.  
  75.     for (int i = 0; i < NUM_OBJECTS; i++) {
  76.         UniqueGO object{new GameObject()};
  77.         dungeon.push_back(std::move(object));
  78.         RawGO raw {dungeon.back().get()};
  79.         ai.push_back(raw);
  80.     }
  81.  
  82.     for (int i = 0; i < NUM_ITERATIONS; i++) {
  83.         for (auto&& raw: ai) {
  84.             GameObject& object = *raw;
  85.             object.foo();
  86.         }
  87.     }
  88. }
  89.  
  90. // ---------------------------------------------------------------------------
  91. #include <chrono>
  92. #include <functional>
  93. #include <iostream>
  94.  
  95. int messure(std::function<void()> f) {
  96.     std::chrono::time_point<std::chrono::system_clock> start, end;
  97.     start = std::chrono::system_clock::now();
  98.     f();
  99.     end = std::chrono::system_clock::now();
  100.     return std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count();
  101. }
  102.  
  103. int main() {
  104.     std::cout << "shared_ptr & weak_ptr:    " << messure([](){ return test_shared(); }) << "ms\n";
  105.     std::cout << "sole_ptr & dynamic_ptr:   " << messure([](){ return test_sole(); }) << "ms\n";
  106.     std::cout << "unique_ptr & raw pointer: " << messure([](){ return test_unique(); }) << "ms\n";
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement