File: sorted_string_map_perf.cpp

package info (click to toggle)
mdds 3.1.0-4
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 6,068 kB
  • sloc: cpp: 20,809; sh: 1,369; makefile: 624; python: 603
file content (216 lines) | stat: -rw-r--r-- 5,685 bytes parent folder | download | duplicates (4)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
 *
 * Copyright (c) 2014 Kohei Yoshida
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 ************************************************************************/

#include "../include/mdds/sorted_string_map.hpp"
#include "../include/mdds/packed_trie_map.hpp"
#include "../include/mdds/global.hpp"

#include <cstdlib>
#include <iostream>
#include <cstring>
#include <unordered_map>

using namespace std;

#include <stdio.h>
#include <string>
#include <sys/time.h>

namespace {

class stack_printer
{
public:
    explicit stack_printer(const char* msg) :
        m_msg(msg)
    {
        fprintf(stdout, "%s: --begin\n", m_msg.c_str());
        m_start_time = getTime();
    }

    ~stack_printer()
    {
        double end_time = getTime();
        fprintf(stdout, "%s: --end (duration: %g sec)\n", m_msg.c_str(), (end_time - m_start_time));
    }

    void printTime(int line) const
    {
        double fEndTime = getTime();
        fprintf(stdout, "%s: --(%d) (duration: %g sec)\n", m_msg.c_str(), line, (fEndTime - m_start_time));
    }

private:
    double getTime() const
    {
        timeval tv;
        gettimeofday(&tv, NULL);
        return tv.tv_sec + tv.tv_usec / 1000000.0;
    }

    ::std::string m_msg;
    double m_start_time;
};

}

typedef mdds::sorted_string_map<int> map_type;

map_type::entry entries[] =
{
    { MDDS_ASCII("aaron"),     0 },
    { MDDS_ASCII("al"),        1 },
    { MDDS_ASCII("aldi"),      2 },
    { MDDS_ASCII("andy"),      3 },
    { MDDS_ASCII("bison"),     4 },
    { MDDS_ASCII("bruce"),     5 },
    { MDDS_ASCII("charlie"),   6 },
    { MDDS_ASCII("charlotte"), 7 },
    { MDDS_ASCII("david"),     8 },
    { MDDS_ASCII("dove"),      9 },
    { MDDS_ASCII("e"),        10 },
    { MDDS_ASCII("eva"),      11 },
};

typedef mdds::draft::packed_trie_map<int> trie_map_type;

trie_map_type::entry trie_entries[] =
{
    { MDDS_ASCII("aaron"),     0 },
    { MDDS_ASCII("al"),        1 },
    { MDDS_ASCII("aldi"),      2 },
    { MDDS_ASCII("andy"),      3 },
    { MDDS_ASCII("bison"),     4 },
    { MDDS_ASCII("bruce"),     5 },
    { MDDS_ASCII("charlie"),   6 },
    { MDDS_ASCII("charlotte"), 7 },
    { MDDS_ASCII("david"),     8 },
    { MDDS_ASCII("dove"),      9 },
    { MDDS_ASCII("e"),        10 },
    { MDDS_ASCII("eva"),      11 },
};

typedef std::unordered_map<std::string, int> hashmap_type;

void init_hash_map(hashmap_type& hm)
{
    size_t n = sizeof(entries) / sizeof(entries[0]);
    const map_type::entry* p = entries;
    const map_type::entry* pend = p + n;
    for (; p != pend; ++p)
        hm.insert(hashmap_type::value_type(p->key, p->value));
}

void run(map_type& sm, const char* input)
{
    static int sum = 0;
    int type = sm.find(input, strlen(input));
    sum += type;
}

void run(trie_map_type& sm, const char* input)
{
    static int sum = 0;
    int type = sm.find(input, strlen(input));
    sum += type;
}

void run_hash(hashmap_type& hm, const char* input)
{
    static int sum = 0;
    int type = -1;
    hashmap_type::const_iterator it = hm.find(input);
    if (it != hm.end())
        type = it->second;
    sum += type;
}

const char* tests[] = {
    "aaron"
    "al",
    "aldi",
    "andy",
    "andy1",
    "andy13",
    "bison",
    "blah",
    "bruce",
    "bruce",
    "charlie",
    "charlotte",
    "david",
    "dove",
    "e",
    "eva"
};

int main()
{
    static const size_t repeat_count = 10000000;

    map_type sorted_map(entries, std::size(entries), -1);
    size_t n = sorted_map.size();
    cout << "entry count = " << n << endl;

    {
        stack_printer __stack_printer__("sorted map");
        for (size_t rep = 0; rep < repeat_count; ++rep)
        {
            for (size_t i = 0; i < n; ++i)
                run(sorted_map, tests[i]);
        }
    }

    trie_map_type trie_map(trie_entries, std::size(trie_entries), -1);
    n = trie_map.size();
    cout << "entry count = " << n << endl;

    {
        stack_printer __stack_printer__("trie map");
        for (size_t rep = 0; rep < repeat_count; ++rep)
        {
            for (size_t i = 0; i < n; ++i)
                run(trie_map, tests[i]);
        }
    }

    hashmap_type hm;
    init_hash_map(hm);
    {
        stack_printer __stack_printer__("hash map");
        for (size_t rep = 0; rep < repeat_count; ++rep)
        {
            for (size_t i = 0; i < n; ++i)
                run_hash(hm, tests[i]);
        }
    }

    return EXIT_SUCCESS;
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */