~ubuntu-branches/ubuntu/trusty/mdds/trusty-proposed

« back to all changes in this revision

Viewing changes to src/multi_type_vector_test_perf.cpp

  • Committer: Package Import Robot
  • Author(s): Rene Engelhard
  • Date: 2013-10-21 21:49:59 UTC
  • mfrom: (4.1.12 experimental)
  • Revision ID: package-import@ubuntu.com-20131021214959-eulcfavx98ntwnuu
Tags: 0.9.1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*************************************************************************
 
2
 *
 
3
 * Copyright (c) 2011-2013 Kohei Yoshida
 
4
 *
 
5
 * Permission is hereby granted, free of charge, to any person
 
6
 * obtaining a copy of this software and associated documentation
 
7
 * files (the "Software"), to deal in the Software without
 
8
 * restriction, including without limitation the rights to use,
 
9
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 
10
 * copies of the Software, and to permit persons to whom the
 
11
 * Software is furnished to do so, subject to the following
 
12
 * conditions:
 
13
 *
 
14
 * The above copyright notice and this permission notice shall be
 
15
 * included in all copies or substantial portions of the Software.
 
16
 *
 
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 
19
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
20
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 
21
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 
22
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 
24
 * OTHER DEALINGS IN THE SOFTWARE.
 
25
 *
 
26
 ************************************************************************/
 
27
 
 
28
#include "test_global.hpp"
 
29
 
 
30
#include <mdds/multi_type_vector.hpp>
 
31
#include <mdds/multi_type_vector_trait.hpp>
 
32
 
 
33
#include <cassert>
 
34
#include <sstream>
 
35
#include <vector>
 
36
#include <deque>
 
37
 
 
38
#include <boost/ptr_container/ptr_vector.hpp>
 
39
#include <boost/noncopyable.hpp>
 
40
 
 
41
#define ARRAY_SIZE(x) sizeof(x)/sizeof(x[0])
 
42
 
 
43
using namespace std;
 
44
using namespace mdds;
 
45
 
 
46
namespace {
 
47
 
 
48
typedef mdds::multi_type_vector<mdds::mtv::element_block_func> mtv_type;
 
49
 
 
50
void mtv_perf_test_block_position_lookup()
 
51
{
 
52
    size_t n = 24000;
 
53
 
 
54
    {
 
55
        // Default insertion which always looks up the right element block
 
56
        // from the position of the first block.  As such, as the block size
 
57
        // grows, so does the time it takes to search for the right block.
 
58
 
 
59
        mtv_type db(n*2);
 
60
        double val1 = 1.1;
 
61
        int val2 = 23;
 
62
        stack_printer __stack_printer__("::mtv_perf_test_block_position_lookup::default insertion");
 
63
        for (size_t i = 0; i < n; ++i)
 
64
        {
 
65
            size_t pos1 = i*2, pos2 = i*2 + 1;
 
66
            db.set(pos1, val1);
 
67
            db.set(pos2, val2);
 
68
        }
 
69
    }
 
70
 
 
71
    {
 
72
        // As a solution for this, we can use an iterator to specify the start
 
73
        // position, which eliminates the above scalability problem nicely.
 
74
 
 
75
        mtv_type db(n*2);
 
76
        mtv_type::iterator pos_hint = db.begin();
 
77
        double val1 = 1.1;
 
78
        int val2 = 23;
 
79
        stack_printer __stack_printer__("::mtv_perf_test_block_position_lookup::insertion with position hint");
 
80
        for (size_t i = 0; i < n; ++i)
 
81
        {
 
82
            size_t pos1 = i*2, pos2 = i*2 + 1;
 
83
            pos_hint = db.set(pos_hint, pos1, val1);
 
84
            pos_hint = db.set(pos_hint, pos2, val2);
 
85
        }
 
86
    }
 
87
}
 
88
 
 
89
void mtv_perf_test_insert_via_position_object()
 
90
{
 
91
    size_t data_size = 80000;
 
92
    mtv_type db(data_size);
 
93
    {
 
94
        stack_printer __stack_printer__("::mtv_perf_test_insert_via_position_object initialize mtv.");
 
95
        mtv_type::iterator it = db.begin();
 
96
        for (size_t i = 0, n = db.size() / 2; i < n; ++i)
 
97
        {
 
98
            it = db.set(it, i*2, 1.1);
 
99
        }
 
100
    }
 
101
 
 
102
    mtv_type db2 = db;
 
103
    {
 
104
        stack_printer __stack_printer__("::mtv_perf_test_insert_via_position_object insert with position hint.");
 
105
        mtv_type::iterator it = db2.begin();
 
106
        for (size_t i = 0, n = db2.size(); i < n; ++i)
 
107
        {
 
108
            it = db2.set(it, i, string("foo"));
 
109
        }
 
110
    }
 
111
 
 
112
    db2 = db;
 
113
    {
 
114
        stack_printer __stack_printer__("::mtv_perf_test_insert_via_position_object insert via position object.");
 
115
        mtv_type::position_type pos = db2.position(0);
 
116
        for (; pos.first != db2.end(); pos = mtv_type::next_position(pos))
 
117
        {
 
118
            size_t log_pos = mtv_type::logical_position(pos);
 
119
            pos.first = db2.set(pos.first, log_pos, string("foo"));
 
120
            pos.second = log_pos - pos.first->position;
 
121
        }
 
122
    }
 
123
}
 
124
 
 
125
}
 
126
 
 
127
int main (int argc, char **argv)
 
128
{
 
129
    mtv_perf_test_block_position_lookup();
 
130
    mtv_perf_test_insert_via_position_object();
 
131
    return EXIT_SUCCESS;
 
132
}