0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
// This file contains benchmarks for comparing QVector against std::vector
|
|
42 |
|
|
43 |
#include <QtCore>
|
|
44 |
#include <QVector>
|
|
45 |
#include <vector>
|
|
46 |
|
|
47 |
#include <qtest.h>
|
|
48 |
|
|
49 |
template <typename T> // T is the item type
|
|
50 |
class UseCases {
|
|
51 |
public:
|
|
52 |
virtual ~UseCases() {}
|
|
53 |
|
|
54 |
// Use case: Insert \a size items into the vector.
|
|
55 |
virtual void insert(int size) = 0;
|
|
56 |
|
|
57 |
// Use case: Lookup \a size items from the vector.
|
|
58 |
virtual void lookup(int size) = 0;
|
|
59 |
};
|
|
60 |
|
|
61 |
template <typename T>
|
|
62 |
static T * f(T *ts) // dummy function to prevent code from being optimized away by the compiler
|
|
63 |
{
|
|
64 |
return ts;
|
|
65 |
}
|
|
66 |
|
|
67 |
// This subclass implements the use cases using QVector as efficiently as possible.
|
|
68 |
template <typename T>
|
|
69 |
class UseCases_QVector : public UseCases<T>
|
|
70 |
{
|
|
71 |
void insert(int size)
|
|
72 |
{
|
|
73 |
QVector<T> v;
|
|
74 |
T t;
|
|
75 |
QBENCHMARK {
|
|
76 |
for (int i = 0; i < size; ++i)
|
|
77 |
v.append(t);
|
|
78 |
}
|
|
79 |
}
|
|
80 |
|
|
81 |
void lookup(int size)
|
|
82 |
{
|
|
83 |
QVector<T> v;
|
|
84 |
|
|
85 |
T t;
|
|
86 |
for (int i = 0; i < size; ++i)
|
|
87 |
v.append(t);
|
|
88 |
|
|
89 |
T *ts = new T[size];
|
|
90 |
QBENCHMARK {
|
|
91 |
for (int i = 0; i < size; ++i)
|
|
92 |
ts[i] = v.value(i);
|
|
93 |
}
|
|
94 |
f<T>(ts);
|
|
95 |
delete[] ts;
|
|
96 |
}
|
|
97 |
};
|
|
98 |
|
|
99 |
// This subclass implements the use cases using std::vector as efficiently as possible.
|
|
100 |
template <typename T>
|
|
101 |
class UseCases_stdvector : public UseCases<T>
|
|
102 |
{
|
|
103 |
void insert(int size)
|
|
104 |
{
|
|
105 |
std::vector<T> v;
|
|
106 |
T t;
|
|
107 |
QBENCHMARK {
|
|
108 |
for (int i = 0; i < size; ++i)
|
|
109 |
v.push_back(t);
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
void lookup(int size)
|
|
114 |
{
|
|
115 |
std::vector<T> v;
|
|
116 |
|
|
117 |
T t;
|
|
118 |
for (int i = 0; i < size; ++i)
|
|
119 |
v.push_back(t);
|
|
120 |
|
|
121 |
T *ts = new T[size];
|
|
122 |
QBENCHMARK {
|
|
123 |
for (int i = 0; i < size; ++i)
|
|
124 |
ts[i] = v[i];
|
|
125 |
}
|
|
126 |
f<T>(ts);
|
|
127 |
delete[] ts;
|
|
128 |
}
|
|
129 |
};
|
|
130 |
|
|
131 |
struct Large { // A "large" item type
|
|
132 |
int x[1000];
|
|
133 |
};
|
|
134 |
|
|
135 |
class tst_vector_vs_std : public QObject
|
|
136 |
{
|
|
137 |
Q_OBJECT
|
|
138 |
public:
|
|
139 |
tst_vector_vs_std()
|
|
140 |
{
|
|
141 |
useCases_QVector_int = new UseCases_QVector<int>;
|
|
142 |
useCases_stdvector_int = new UseCases_stdvector<int>;
|
|
143 |
|
|
144 |
useCases_QVector_Large = new UseCases_QVector<Large>;
|
|
145 |
useCases_stdvector_Large = new UseCases_stdvector<Large>;
|
|
146 |
}
|
|
147 |
|
|
148 |
private:
|
|
149 |
UseCases<int> *useCases_QVector_int;
|
|
150 |
UseCases<int> *useCases_stdvector_int;
|
|
151 |
UseCases<Large> *useCases_QVector_Large;
|
|
152 |
UseCases<Large> *useCases_stdvector_Large;
|
|
153 |
|
|
154 |
private slots:
|
|
155 |
void insert_int_data();
|
|
156 |
void insert_int();
|
|
157 |
void insert_Large_data();
|
|
158 |
void insert_Large();
|
|
159 |
void lookup_int_data();
|
|
160 |
void lookup_int();
|
|
161 |
void lookup_Large_data();
|
|
162 |
void lookup_Large();
|
|
163 |
};
|
|
164 |
|
|
165 |
void tst_vector_vs_std::insert_int_data()
|
|
166 |
{
|
|
167 |
QTest::addColumn<bool>("useStd");
|
|
168 |
QTest::addColumn<int>("size");
|
|
169 |
|
|
170 |
for (int size = 10; size < 20000; size += 100) {
|
|
171 |
const QByteArray sizeString = QByteArray::number(size);
|
|
172 |
QTest::newRow(("std::vector-int--" + sizeString).constData()) << true << size;
|
|
173 |
QTest::newRow(("QVector-int--" + sizeString).constData()) << false << size;
|
|
174 |
}
|
|
175 |
}
|
|
176 |
|
|
177 |
void tst_vector_vs_std::insert_int()
|
|
178 |
{
|
|
179 |
QFETCH(bool, useStd);
|
|
180 |
QFETCH(int, size);
|
|
181 |
|
|
182 |
if (useStd)
|
|
183 |
useCases_stdvector_int->insert(size);
|
|
184 |
else
|
|
185 |
useCases_QVector_int->insert(size);
|
|
186 |
}
|
|
187 |
|
|
188 |
void tst_vector_vs_std::insert_Large_data()
|
|
189 |
{
|
|
190 |
QTest::addColumn<bool>("useStd");
|
|
191 |
QTest::addColumn<int>("size");
|
|
192 |
|
|
193 |
for (int size = 10; size < 20000; size += 100) {
|
|
194 |
const QByteArray sizeString = QByteArray::number(size);
|
|
195 |
QTest::newRow(("std::vector-Large--" + sizeString).constData()) << true << size;
|
|
196 |
QTest::newRow(("QVector-Large--" + sizeString).constData()) << false << size;
|
|
197 |
}
|
|
198 |
}
|
|
199 |
|
|
200 |
void tst_vector_vs_std::insert_Large()
|
|
201 |
{
|
|
202 |
QFETCH(bool, useStd);
|
|
203 |
QFETCH(int, size);
|
|
204 |
|
|
205 |
if (useStd)
|
|
206 |
useCases_stdvector_Large->insert(size);
|
|
207 |
else
|
|
208 |
useCases_QVector_Large->insert(size);
|
|
209 |
}
|
|
210 |
|
|
211 |
void tst_vector_vs_std::lookup_int_data()
|
|
212 |
{
|
|
213 |
QTest::addColumn<bool>("useStd");
|
|
214 |
QTest::addColumn<int>("size");
|
|
215 |
|
|
216 |
for (int size = 10; size < 20000; size += 100) {
|
|
217 |
const QByteArray sizeString = QByteArray::number(size);
|
|
218 |
QTest::newRow(("std::vector-int--" + sizeString).constData()) << true << size;
|
|
219 |
QTest::newRow(("QVector-int--" + sizeString).constData()) << false << size;
|
|
220 |
}
|
|
221 |
}
|
|
222 |
|
|
223 |
void tst_vector_vs_std::lookup_int()
|
|
224 |
{
|
|
225 |
QFETCH(bool, useStd);
|
|
226 |
QFETCH(int, size);
|
|
227 |
|
|
228 |
if (useStd)
|
|
229 |
useCases_stdvector_int->lookup(size);
|
|
230 |
else
|
|
231 |
useCases_QVector_int->lookup(size);
|
|
232 |
}
|
|
233 |
|
|
234 |
void tst_vector_vs_std::lookup_Large_data()
|
|
235 |
{
|
|
236 |
QTest::addColumn<bool>("useStd");
|
|
237 |
QTest::addColumn<int>("size");
|
|
238 |
|
|
239 |
for (int size = 10; size < 20000; size += 100) {
|
|
240 |
const QByteArray sizeString = QByteArray::number(size);
|
|
241 |
QTest::newRow(("std::vector-Large--" + sizeString).constData()) << true << size;
|
|
242 |
QTest::newRow(("QVector-Large--" + sizeString).constData()) << false << size;
|
|
243 |
}
|
|
244 |
}
|
|
245 |
|
|
246 |
void tst_vector_vs_std::lookup_Large()
|
|
247 |
{
|
|
248 |
QFETCH(bool, useStd);
|
|
249 |
QFETCH(int, size);
|
|
250 |
|
|
251 |
if (useStd)
|
|
252 |
useCases_stdvector_Large->lookup(size);
|
|
253 |
else
|
|
254 |
useCases_QVector_Large->lookup(size);
|
|
255 |
}
|
|
256 |
|
|
257 |
QTEST_MAIN(tst_vector_vs_std)
|
|
258 |
#include "main.moc"
|