| 1 | #include <boost/shared_ptr.hpp>
|
|---|
| 2 | #include <boost/weak_ptr.hpp>
|
|---|
| 3 | #include <functional>
|
|---|
| 4 | #include <iostream>
|
|---|
| 5 | #include <numeric>
|
|---|
| 6 | #include <vector>
|
|---|
| 7 | #include <time.h>
|
|---|
| 8 |
|
|---|
| 9 | namespace
|
|---|
| 10 | {
|
|---|
| 11 |
|
|---|
| 12 | const long long NS_IN_SEC = 1000000000;
|
|---|
| 13 |
|
|---|
| 14 | long long getTimeNS()
|
|---|
| 15 | {
|
|---|
| 16 | timespec ts;
|
|---|
| 17 | clock_gettime(CLOCK_REALTIME, &ts);
|
|---|
| 18 | return ((ts.tv_sec * NS_IN_SEC) + ts.tv_nsec);
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | long long numCallsToTestDestructor = 0;
|
|---|
| 22 |
|
|---|
| 23 | struct Test
|
|---|
| 24 | {
|
|---|
| 25 | Test()
|
|---|
| 26 | {
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | ~Test()
|
|---|
| 30 | {
|
|---|
| 31 | ++numCallsToTestDestructor;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | };
|
|---|
| 35 |
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | int main(int argc, char** argv)
|
|---|
| 39 | {
|
|---|
| 40 | const int iterations = 10000;
|
|---|
| 41 | std::vector<long long> deltaNSVector;
|
|---|
| 42 | deltaNSVector.reserve(iterations);
|
|---|
| 43 |
|
|---|
| 44 | for (int i = 0; i < iterations; ++i)
|
|---|
| 45 | {
|
|---|
| 46 | const long long startTimeNS = getTimeNS();
|
|---|
| 47 | boost::shared_ptr<Test> p(new Test);
|
|---|
| 48 | for (int j = 0; j < 10; ++j)
|
|---|
| 49 | {
|
|---|
| 50 | boost::shared_ptr<Test> p2(p);
|
|---|
| 51 | boost::weak_ptr<Test> pw(p);
|
|---|
| 52 | boost::shared_ptr<Test> p3(pw);
|
|---|
| 53 | }
|
|---|
| 54 | const long long endTimeNS = getTimeNS();
|
|---|
| 55 | deltaNSVector.push_back(endTimeNS - startTimeNS);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | const long long sum = std::accumulate(deltaNSVector.begin(), deltaNSVector.end(), 0);
|
|---|
| 59 | const double average = ((double)sum) / iterations;
|
|---|
| 60 | std::cout << "average time ns = " << average << std::endl;
|
|---|
| 61 | std::cout << "numCallsToTestDestructor = " << numCallsToTestDestructor << std::endl;
|
|---|
| 62 |
|
|---|
| 63 | return 0;
|
|---|
| 64 | }
|
|---|