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
|
// Copyright 2009-2021 The Mumble Developers. All rights reserved.
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file at the root of the
// Mumble source tree or at <https://www.mumble.info/LICENSE>.
/**
* Benchmark of storage mechanisms; array, vector, Qt collections.
*/
#include <QtCore>
#include <vector>
#include "Timer.h"
using namespace std;
#define SL 10000
#define NE 300
template< class T > class QListSet {
public:
QList< T > l;
void insert(const T &i) {
if (!l.contains(i))
l.append(i);
}
void remove(const T &i) { l.removeAll(i); }
};
int main(int argc, char **argv) {
Timer t;
quint64 usins, usiter;
int sum;
{
t.restart();
QSet< int > s;
s.reserve(NE);
for (int i = 0; i < NE; i++)
s.insert(i);
usins = t.restart();
sum = 0;
for (int j = 0; j < SL; j++)
foreach (int v, s)
sum += v;
usiter = t.restart();
}
qWarning("QSet: %lldus insert, %lldus iterate: %d", usins, usiter, sum);
{
t.restart();
QList< int > s;
for (int i = 0; i < NE; i++)
s.append(i);
usins = t.restart();
sum = 0;
for (int j = 0; j < SL; j++)
foreach (int v, s)
sum += v;
usiter = t.restart();
}
qWarning("QList: %lldus insert, %lldus iterate: %d", usins, usiter, sum);
{
t.restart();
QVector< int > s;
s.reserve(NE);
for (int i = 0; i < NE; i++)
s.append(i);
usins = t.restart();
sum = 0;
for (int j = 0; j < SL; j++)
foreach (int v, s)
sum += v;
usiter = t.restart();
}
qWarning("QVector: %lldus insert, %lldus iterate: %d", usins, usiter, sum);
{
t.restart();
vector< int > s;
for (int i = 0; i < NE; i++)
s.push_back(i);
usins = t.restart();
sum = 0;
int sz = s.size();
for (int j = 0; j < SL; j++)
for (int i = 0; i < sz; i++)
sum += s[i];
usiter = t.restart();
}
qWarning("std::vector: %lldus insert, %lldus iterate: %d", usins, usiter, sum);
{
t.restart();
int s[NE];
for (int i = 0; i < NE; i++)
s[i] = i;
usins = t.restart();
sum = 0;
for (int j = 0; j < SL; j++)
for (int i = 0; i < NE; i++)
sum += s[i];
usiter = t.restart();
}
qWarning("c99 alloc: %lldus insert, %lldus iterate: %d", usins, usiter, sum);
{
t.restart();
QListSet< int > s;
for (int i = 0; i < NE; i++)
s.insert(i);
usins = t.restart();
sum = 0;
for (int j = 0; j < SL; j++)
foreach (int v, s.l)
sum += v;
usiter = t.restart();
}
qWarning("QListSet: %lldus insert, %lldus iterate: %d", usins, usiter, sum);
}
|