Ticket #6308: test.cpp

File test.cpp, 1.3 KB (added by aaron.riekenberg@…, 11 years ago)

Benchmark program

Line 
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
9namespace
10{
11
12const long long NS_IN_SEC = 1000000000;
13
14long 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
21long long numCallsToTestDestructor = 0;
22
23struct Test
24{
25 Test()
26 {
27 }
28
29 ~Test()
30 {
31 ++numCallsToTestDestructor;
32 }
33
34};
35
36}
37
38int 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}