author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 15 Mar 2010 12:43:09 +0200 | |
branch | RCL_3 |
changeset 6 | dee5afe5301f |
parent 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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> |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
62 |
T * f(T *ts) // dummy function to prevent code from being optimized away by the compiler |
0 | 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; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
84 |
|
0 | 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; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
116 |
|
0 | 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 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
135 |
// Symbian devices typically have limited memory |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
136 |
#ifdef Q_OS_SYMBIAN |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
137 |
# define LARGE_MAX_SIZE 2000 |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
138 |
#else |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
139 |
# define LARGE_MAX_SIZE 20000 |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
140 |
#endif |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
141 |
|
0 | 142 |
class tst_vector_vs_std : public QObject |
143 |
{ |
|
144 |
Q_OBJECT |
|
145 |
public: |
|
146 |
tst_vector_vs_std() |
|
147 |
{ |
|
148 |
useCases_QVector_int = new UseCases_QVector<int>; |
|
149 |
useCases_stdvector_int = new UseCases_stdvector<int>; |
|
150 |
||
151 |
useCases_QVector_Large = new UseCases_QVector<Large>; |
|
152 |
useCases_stdvector_Large = new UseCases_stdvector<Large>; |
|
153 |
} |
|
154 |
||
155 |
private: |
|
156 |
UseCases<int> *useCases_QVector_int; |
|
157 |
UseCases<int> *useCases_stdvector_int; |
|
158 |
UseCases<Large> *useCases_QVector_Large; |
|
159 |
UseCases<Large> *useCases_stdvector_Large; |
|
160 |
||
161 |
private slots: |
|
162 |
void insert_int_data(); |
|
163 |
void insert_int(); |
|
164 |
void insert_Large_data(); |
|
165 |
void insert_Large(); |
|
166 |
void lookup_int_data(); |
|
167 |
void lookup_int(); |
|
168 |
void lookup_Large_data(); |
|
169 |
void lookup_Large(); |
|
170 |
}; |
|
171 |
||
172 |
void tst_vector_vs_std::insert_int_data() |
|
173 |
{ |
|
174 |
QTest::addColumn<bool>("useStd"); |
|
175 |
QTest::addColumn<int>("size"); |
|
176 |
||
177 |
for (int size = 10; size < 20000; size += 100) { |
|
178 |
const QByteArray sizeString = QByteArray::number(size); |
|
179 |
QTest::newRow(("std::vector-int--" + sizeString).constData()) << true << size; |
|
180 |
QTest::newRow(("QVector-int--" + sizeString).constData()) << false << size; |
|
181 |
} |
|
182 |
} |
|
183 |
||
184 |
void tst_vector_vs_std::insert_int() |
|
185 |
{ |
|
186 |
QFETCH(bool, useStd); |
|
187 |
QFETCH(int, size); |
|
188 |
||
189 |
if (useStd) |
|
190 |
useCases_stdvector_int->insert(size); |
|
191 |
else |
|
192 |
useCases_QVector_int->insert(size); |
|
193 |
} |
|
194 |
||
195 |
void tst_vector_vs_std::insert_Large_data() |
|
196 |
{ |
|
197 |
QTest::addColumn<bool>("useStd"); |
|
198 |
QTest::addColumn<int>("size"); |
|
199 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
200 |
for (int size = 10; size < LARGE_MAX_SIZE; size += 100) { |
0 | 201 |
const QByteArray sizeString = QByteArray::number(size); |
202 |
QTest::newRow(("std::vector-Large--" + sizeString).constData()) << true << size; |
|
203 |
QTest::newRow(("QVector-Large--" + sizeString).constData()) << false << size; |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
void tst_vector_vs_std::insert_Large() |
|
208 |
{ |
|
209 |
QFETCH(bool, useStd); |
|
210 |
QFETCH(int, size); |
|
211 |
||
212 |
if (useStd) |
|
213 |
useCases_stdvector_Large->insert(size); |
|
214 |
else |
|
215 |
useCases_QVector_Large->insert(size); |
|
216 |
} |
|
217 |
||
218 |
void tst_vector_vs_std::lookup_int_data() |
|
219 |
{ |
|
220 |
QTest::addColumn<bool>("useStd"); |
|
221 |
QTest::addColumn<int>("size"); |
|
222 |
||
223 |
for (int size = 10; size < 20000; size += 100) { |
|
224 |
const QByteArray sizeString = QByteArray::number(size); |
|
225 |
QTest::newRow(("std::vector-int--" + sizeString).constData()) << true << size; |
|
226 |
QTest::newRow(("QVector-int--" + sizeString).constData()) << false << size; |
|
227 |
} |
|
228 |
} |
|
229 |
||
230 |
void tst_vector_vs_std::lookup_int() |
|
231 |
{ |
|
232 |
QFETCH(bool, useStd); |
|
233 |
QFETCH(int, size); |
|
234 |
||
235 |
if (useStd) |
|
236 |
useCases_stdvector_int->lookup(size); |
|
237 |
else |
|
238 |
useCases_QVector_int->lookup(size); |
|
239 |
} |
|
240 |
||
241 |
void tst_vector_vs_std::lookup_Large_data() |
|
242 |
{ |
|
243 |
QTest::addColumn<bool>("useStd"); |
|
244 |
QTest::addColumn<int>("size"); |
|
245 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
246 |
for (int size = 10; size < LARGE_MAX_SIZE; size += 100) { |
0 | 247 |
const QByteArray sizeString = QByteArray::number(size); |
248 |
QTest::newRow(("std::vector-Large--" + sizeString).constData()) << true << size; |
|
249 |
QTest::newRow(("QVector-Large--" + sizeString).constData()) << false << size; |
|
250 |
} |
|
251 |
} |
|
252 |
||
253 |
void tst_vector_vs_std::lookup_Large() |
|
254 |
{ |
|
255 |
QFETCH(bool, useStd); |
|
256 |
QFETCH(int, size); |
|
257 |
||
258 |
if (useStd) |
|
259 |
useCases_stdvector_Large->lookup(size); |
|
260 |
else |
|
261 |
useCases_QVector_Large->lookup(size); |
|
262 |
} |
|
263 |
||
264 |
QTEST_MAIN(tst_vector_vs_std) |
|
265 |
#include "main.moc" |