summaryrefslogtreecommitdiff
path: root/tools/container_test.cpp
blob: 3b65dad7c406993656c75e3d11cab4f010c1cb71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132

#include <base_struct.h>
#include <boost/ptr_container/ptr_vector.hpp>
#include <deque>
#include <dlist.h>
#include <time.h>
#include <common.h>

#define TEST_NODES      100000000


//typedef std::vector<EDA_ITEM*>       EDA_ITEMV;
//typedef std::deque<EDA_ITEM*>        EDA_ITEMV;
typedef boost::ptr_vector<EDA_ITEM>  EDA_ITEMV;

class MY_ITEM : public  EDA_ITEM
{
public:

    MY_ITEM( KICAD_T id ) :
        EDA_ITEM( id )
    {}


#if defined(DEBUG)
    void Show( int nestLevel, std::ostream& os ) const
    {
        ShowDummy( os );
    }
#endif
};


void heap_warm_up();

int main( int argc, char** argv )
{
    EDA_ITEMV       v;
    DLIST<EDA_ITEM> dlist;

    unsigned vAllocStart;
    unsigned vAllocStop;
    unsigned vIterateStart;
    unsigned vIterateStop;

    unsigned dAllocStart;
    unsigned dAllocStop;
    unsigned dIterateStart;
    unsigned dIterateStop;

    heap_warm_up();

    vAllocStart = GetRunningMicroSecs();

    for( int i=0;  i<TEST_NODES;  ++i )
    {
        v.push_back( new MY_ITEM( NOT_USED ) );
    }

    vAllocStop = GetRunningMicroSecs();
    vIterateStart = vAllocStop;

    for( EDA_ITEMV::const_iterator it = v.begin();  it != v.end();  ++it )
    {
        if( it->Type() == -22 )
        {
            printf( "never this\n" );
            break;
        }
    }

    vIterateStop = GetRunningMicroSecs();

#if 0
    for( int i=0; i<TEST_NODES;  ++i )
    {
        delete v[i];
    }
#endif

    v.clear();


    dAllocStart = GetRunningMicroSecs();

    for( int i=0;  i<TEST_NODES;  ++i )
    {
        dlist.PushBack( new MY_ITEM( NOT_USED ) );
    }

    dAllocStop = GetRunningMicroSecs();
    dIterateStart = dAllocStop;

    for( const EDA_ITEM* it = dlist;  it;  it = it->Next() )
    {
        if( it->Type() == -22 )
        {
            printf( "never this\n" );
            break;
        }
    }

    dIterateStop = GetRunningMicroSecs();

    printf( "vector alloc: %u usecs  iterate: %u usecs\n",
            vAllocStop - vAllocStart,
            vIterateStop - vIterateStart );

    printf( "dlist alloc: %u usecs  iterate: %u usecs\n",
            dAllocStop - dAllocStart,
            dIterateStop - dIterateStart );
}


void heap_warm_up()
{
    // dry run allocate enough object for process to obtain all memory needed

    EDA_ITEMV  vec;

    for( int i=0; i<TEST_NODES;  ++i )
    {
        vec.push_back( new MY_ITEM( NOT_USED ) );
    }

    for( int i=0; i<TEST_NODES;  ++i )
    {
        // delete vec[i];
    }

    vec.clear();
}