1 #include <boost/config.hpp>
2 
3 #if defined(BOOST_MSVC)
4 #pragma warning(disable: 4786)  // identifier truncated in debug info
5 #pragma warning(disable: 4710)  // function not inlined
6 #pragma warning(disable: 4711)  // function selected for automatic inline expansion
7 #pragma warning(disable: 4514)  // unreferenced inline removed
8 #endif
9 
10 //
11 //  weak_ptr_timing_test.cpp
12 //
13 //  Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
14 //  Copyright 2005 Peter Dimov
15 //
16 // Distributed under the Boost Software License, Version 1.0. (See
17 // accompanying file LICENSE_1_0.txt or copy at
18 // http://www.boost.org/LICENSE_1_0.txt)
19 //
20 
21 #include <boost/shared_ptr.hpp>
22 #include <boost/weak_ptr.hpp>
23 
24 #include <vector>
25 #include <cstdio>
26 #include <ctime>
27 #include <cstdlib>
28 
29 //
30 
31 int const n = 29000;
32 int const k = 2048;
33 
test(std::vector<boost::shared_ptr<int>> & v)34 void test( std::vector< boost::shared_ptr<int> > & v )
35 {
36     using namespace std; // printf, rand
37 
38     std::vector< boost::weak_ptr<int> > w( v.begin(), v.end() );
39 
40     int s = 0, r = 0;
41 
42     for( int i = 0; i < n; ++i )
43     {
44         // randomly kill a pointer
45 
46         v[ rand() % k ].reset();
47 
48         for( int j = 0; j < k; ++j )
49         {
50             if( boost::shared_ptr<int> px = w[ j ].lock() )
51             {
52                 ++s;
53             }
54             else
55             {
56                 ++r;
57                 w[ j ] = v[ rand() % k ];
58             }
59         }
60     }
61 
62     printf( "\n%d locks, %d rebinds.", s, r );
63 }
64 
main()65 int main()
66 {
67     using namespace std; // printf, clock_t, clock
68 
69     std::vector< boost::shared_ptr<int> > v( k );
70 
71     for( int i = 0; i < k; ++i )
72     {
73         v[ i ].reset( new int( 0 ) );
74     }
75 
76     clock_t t = clock();
77 
78     test( v );
79 
80     t = clock() - t;
81 
82     printf( "\n\n%.3f seconds.\n", static_cast<double>( t ) / CLOCKS_PER_SEC );
83 
84     return 0;
85 }
86