source: trunk/examples/qtestlib/tutorial5/containers.cpp@ 1030

Last change on this file since 1030 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 7.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the test suite of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:BSD$
10** You may use this file under the terms of the BSD license as follows:
11**
12** "Redistribution and use in source and binary forms, with or without
13** modification, are permitted provided that the following conditions are
14** met:
15** * Redistributions of source code must retain the above copyright
16** notice, this list of conditions and the following disclaimer.
17** * Redistributions in binary form must reproduce the above copyright
18** notice, this list of conditions and the following disclaimer in
19** the documentation and/or other materials provided with the
20** distribution.
21** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
22** the names of its contributors may be used to endorse or promote
23** products derived from this software without specific prior written
24** permission.
25**
26** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40// This file contains benchmarks for comparing QVector against std::vector
41
42#include <QtCore>
43#include <QVector>
44#include <vector>
45
46#include <qtest.h>
47
48template <typename T> // T is the item type
49class UseCases {
50public:
51 virtual ~UseCases() {}
52
53 // Use case: Insert \a size items into the vector.
54 virtual void insert(int size) = 0;
55
56 // Use case: Lookup \a size items from the vector.
57 virtual void lookup(int size) = 0;
58};
59
60template <typename T>
61T * f(T *ts) // dummy function to prevent code from being optimized away by the compiler
62{
63 return ts;
64}
65
66// This subclass implements the use cases using QVector as efficiently as possible.
67template <typename T>
68class UseCases_QVector : public UseCases<T>
69{
70 void insert(int size)
71 {
72 QVector<T> v;
73 T t;
74 QBENCHMARK {
75 for (int i = 0; i < size; ++i)
76 v.append(t);
77 }
78 }
79
80 void lookup(int size)
81 {
82 QVector<T> v;
83
84 T t;
85 for (int i = 0; i < size; ++i)
86 v.append(t);
87
88 T *ts = new T[size];
89 QBENCHMARK {
90 for (int i = 0; i < size; ++i)
91 ts[i] = v.value(i);
92 }
93 f<T>(ts);
94 delete[] ts;
95 }
96};
97
98// This subclass implements the use cases using std::vector as efficiently as possible.
99template <typename T>
100class UseCases_stdvector : public UseCases<T>
101{
102 void insert(int size)
103 {
104 std::vector<T> v;
105 T t;
106 QBENCHMARK {
107 for (int i = 0; i < size; ++i)
108 v.push_back(t);
109 }
110 }
111
112 void lookup(int size)
113 {
114 std::vector<T> v;
115
116 T t;
117 for (int i = 0; i < size; ++i)
118 v.push_back(t);
119
120 T *ts = new T[size];
121 QBENCHMARK {
122 for (int i = 0; i < size; ++i)
123 ts[i] = v[i];
124 }
125 f<T>(ts);
126 delete[] ts;
127 }
128};
129
130struct Large { // A "large" item type
131 int x[1000];
132};
133
134// Symbian devices typically have limited memory
135#ifdef Q_OS_SYMBIAN
136# define LARGE_MAX_SIZE 2000
137#else
138# define LARGE_MAX_SIZE 20000
139#endif
140
141class tst_vector_vs_std : public QObject
142{
143 Q_OBJECT
144public:
145 tst_vector_vs_std()
146 {
147 useCases_QVector_int = new UseCases_QVector<int>;
148 useCases_stdvector_int = new UseCases_stdvector<int>;
149
150 useCases_QVector_Large = new UseCases_QVector<Large>;
151 useCases_stdvector_Large = new UseCases_stdvector<Large>;
152 }
153
154private:
155 UseCases<int> *useCases_QVector_int;
156 UseCases<int> *useCases_stdvector_int;
157 UseCases<Large> *useCases_QVector_Large;
158 UseCases<Large> *useCases_stdvector_Large;
159
160private slots:
161 void insert_int_data();
162 void insert_int();
163 void insert_Large_data();
164 void insert_Large();
165 void lookup_int_data();
166 void lookup_int();
167 void lookup_Large_data();
168 void lookup_Large();
169};
170
171void tst_vector_vs_std::insert_int_data()
172{
173 QTest::addColumn<bool>("useStd");
174 QTest::addColumn<int>("size");
175
176 for (int size = 10; size < 20000; size += 100) {
177 const QByteArray sizeString = QByteArray::number(size);
178 QTest::newRow(("std::vector-int--" + sizeString).constData()) << true << size;
179 QTest::newRow(("QVector-int--" + sizeString).constData()) << false << size;
180 }
181}
182
183void tst_vector_vs_std::insert_int()
184{
185 QFETCH(bool, useStd);
186 QFETCH(int, size);
187
188 if (useStd)
189 useCases_stdvector_int->insert(size);
190 else
191 useCases_QVector_int->insert(size);
192}
193
194void tst_vector_vs_std::insert_Large_data()
195{
196 QTest::addColumn<bool>("useStd");
197 QTest::addColumn<int>("size");
198
199 for (int size = 10; size < LARGE_MAX_SIZE; size += 100) {
200 const QByteArray sizeString = QByteArray::number(size);
201 QTest::newRow(("std::vector-Large--" + sizeString).constData()) << true << size;
202 QTest::newRow(("QVector-Large--" + sizeString).constData()) << false << size;
203 }
204}
205
206void tst_vector_vs_std::insert_Large()
207{
208 QFETCH(bool, useStd);
209 QFETCH(int, size);
210
211 if (useStd)
212 useCases_stdvector_Large->insert(size);
213 else
214 useCases_QVector_Large->insert(size);
215}
216
217//! [1]
218void 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//! [1]
230
231//! [2]
232void tst_vector_vs_std::lookup_int()
233{
234 QFETCH(bool, useStd);
235 QFETCH(int, size);
236
237 if (useStd)
238 useCases_stdvector_int->lookup(size); // Create a std::vector and run the benchmark.
239 else
240 useCases_QVector_int->lookup(size); // Create a QVector and run the benchmark.
241}
242//! [2]
243
244void tst_vector_vs_std::lookup_Large_data()
245{
246 QTest::addColumn<bool>("useStd");
247 QTest::addColumn<int>("size");
248
249 for (int size = 10; size < LARGE_MAX_SIZE; size += 100) {
250 const QByteArray sizeString = QByteArray::number(size);
251 QTest::newRow(("std::vector-Large--" + sizeString).constData()) << true << size;
252 QTest::newRow(("QVector-Large--" + sizeString).constData()) << false << size;
253 }
254}
255
256void tst_vector_vs_std::lookup_Large()
257{
258 QFETCH(bool, useStd);
259 QFETCH(int, size);
260
261 if (useStd)
262 useCases_stdvector_Large->lookup(size);
263 else
264 useCases_QVector_Large->lookup(size);
265}
266
267QTEST_MAIN(tst_vector_vs_std)
268#include "main.moc"
Note: See TracBrowser for help on using the repository browser.