QtBase  v6.3.1
tst_bench_qstringbuilder.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the test suite module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21 ** included in the packaging of this file. Please review the following
22 ** information to ensure the GNU General Public License requirements will
23 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24 **
25 ** $QT_END_LICENSE$
26 **
27 ****************************************************************************/
28 
29 // Select one of the scenarios below
30 #define SCENARIO 1
31 
32 #if SCENARIO == 1
33 // this is the "no harm done" version. Only operator% is active,
34 // with NO_CAST * defined
35 #undef QT_USE_FAST_OPERATOR_PLUS
36 #undef QT_USE_FAST_CONCATENATION
37 #define QT_NO_CAST_FROM_ASCII
38 #define QT_NO_CAST_TO_ASCII
39 #endif
40 
41 
42 #if SCENARIO == 2
43 // this is the "full" version. Operator+ is replaced by a QStringBuilder
44 // based version
45 // with NO_CAST * defined
46 #define QT_USE_FAST_OPERATOR_PLUS
47 #define QT_USE_FAST_CONCATENATION
48 #define QT_NO_CAST_FROM_ASCII
49 #define QT_NO_CAST_TO_ASCII
50 #endif
51 
52 #if SCENARIO == 3
53 // this is the "no harm done" version. Only operator% is active,
54 // with NO_CAST * _not_ defined
55 #undef QT_USE_FAST_OPERATOR_PLUS
56 #undef QT_USE_FAST_CONCATENATION
57 #undef QT_NO_CAST_FROM_ASCII
58 #undef QT_NO_CAST_TO_ASCII
59 #endif
60 
61 #if SCENARIO == 4
62 // this is the "full" version. Operator+ is replaced by a QStringBuilder
63 // based version
64 // with NO_CAST * _not_ defined
65 #define QT_USE_FAST_OPERATOR_PLUS
66 #define QT_USE_FAST_CONCATENATION
67 #undef QT_NO_CAST_FROM_ASCII
68 #undef QT_NO_CAST_TO_ASCII
69 #endif
70 
71 
72 #include <qbytearray.h>
73 #include <qdebug.h>
74 #include <qstring.h>
75 #include <qstringbuilder.h>
76 
77 #include <qtest.h>
78 
79 #include <string>
80 
81 // Select one of the scenarios below
82 #if SCENARIO == 1
83 #define P %
84 #elif SCENARIO == 2
85 #define P +
86 #elif SCENARIO == 3
87 #define P %
88 #elif SCENARIO == 4
89 #define P +
90 #endif
91 
92 #define COMPARE(a, b) QCOMPARE(a, b)
93 //#define COMPARE(a, b)
94 
95 #define SEP(s) qDebug() << "\n\n-------- " s " ---------";
96 
97 #define LITERAL "some string literal"
98 
100 {
101  Q_OBJECT
102 
103 public:
105  : l1literal(LITERAL),
106  l1string(LITERAL),
107  ba(LITERAL),
108  string(l1string),
109  stdstring(LITERAL),
110  achar('c'),
115  {}
116 
117 
118 public:
119  enum { N = 10000 };
120 
122  {
123  int s = 0;
124  for (int i = 0; i < N; ++i) {
125 #if 0
126  s += QString(l1string + l1string).size();
127  s += QString(l1string + l1string + l1string).size();
128  s += QString(l1string + l1string + l1string + l1string).size();
129  s += QString(l1string + l1string + l1string + l1string + l1string).size();
130 #endif
131  s += QString(achar + l1string + achar).size();
132  }
133  return s;
134  }
135 
137  {
138  int s = 0;
139  for (int i = 0; i < N; ++i) {
140 #if 0
141  s += QString(l1literal P l1literal).size();
142  s += QString(l1literal P l1literal P l1literal).size();
143  s += QString(l1literal P l1literal P l1literal P l1literal).size();
144  s += QString(l1literal P l1literal P l1literal P l1literal P l1literal).size();
145 #endif
146  s += QString(achar % l1literal % achar).size();
147  }
148  return s;
149  }
150 
151 private slots:
152 
153  void separator_0() {
154  qDebug() << "\nIn each block the QStringBuilder based result appear first "
155  "(with a 'b_' prefix), QStringBased second ('q_' prefix), std::string "
156  "last ('s_' prefix)\n";
157  }
158 
159  void separator_1() { SEP("literal + literal (builder first)"); }
160 
161  void b_2_l1literal() {
162  QBENCHMARK { r = l1literal P l1literal; }
163  COMPARE(r, r2);
164  }
165  #ifndef QT_NO_CAST_FROM_ASCII
166  void b_l1literal_LITERAL() {
167  QBENCHMARK { r = l1literal P LITERAL; }
168  COMPARE(r, r2);
169  }
170  #endif
171  void q_2_l1string() {
172  QBENCHMARK { r = l1string + l1string; }
173  COMPARE(r, r2);
174  }
175 
176 
177  void separator_2() { SEP("2 strings"); }
178 
179  void b_2_string() {
180  QBENCHMARK { r = string P string; }
181  COMPARE(r, r2);
182  }
183  void q_2_string() {
184  QBENCHMARK { r = string + string; }
185  COMPARE(r, r2);
186  }
187  void s_2_string() {
188  QBENCHMARK { stdr = stdstring + stdstring; }
189  COMPARE(stdr, stdstring + stdstring);
190  }
191 
192 
193  void separator_2b() { SEP("3 strings"); }
194 
195  void b_3_string() {
196  QBENCHMARK { r = string P string P string; }
197  COMPARE(r, r3);
198  }
199  void q_3_string() {
200  QBENCHMARK { r = string + string + string; }
201  COMPARE(r, r3);
202  }
203  void s_3_string() {
204  QBENCHMARK { stdr = stdstring + stdstring + stdstring; }
205  COMPARE(stdr, stdstring + stdstring + stdstring);
206  }
207 
208  void separator_2e() { SEP("4 strings"); }
209 
210  void b_4_string() {
211  QBENCHMARK { r = string P string P string P string; }
212  COMPARE(r, r4);
213  }
214  void q_4_string() {
215  QBENCHMARK { r = string + string + string + string; }
216  COMPARE(r, r4);
217  }
218  void s_4_string() {
219  QBENCHMARK { stdr = stdstring + stdstring + stdstring + stdstring; }
220  COMPARE(stdr, stdstring + stdstring + stdstring + stdstring);
221  }
222 
223 
224 
225  void separator_2a() { SEP("string + literal (builder first)"); }
226 
227  void b_string_l1literal() {
228  QBENCHMARK { r = string % l1literal; }
229  COMPARE(r, r2);
230  }
231  #ifndef QT_NO_CAST_FROM_ASCII
232  void b_string_LITERAL() {
233  QBENCHMARK { r = string P LITERAL; }
234  COMPARE(r, r2);
235  }
236  void b_LITERAL_string() {
237  QBENCHMARK { r = LITERAL P string; }
238  COMPARE(r, r2);
239  }
240  #endif
241  void b_string_l1string() {
242  QBENCHMARK { r = string P l1string; }
243  COMPARE(r, r2);
244  }
245  void q_string_l1literal() {
246  QBENCHMARK { r = string + l1string; }
247  COMPARE(r, r2);
248  }
249  void q_string_l1string() {
250  QBENCHMARK { r = string + l1string; }
251  COMPARE(r, r2);
252  }
253  void s_LITERAL_string() {
254  QBENCHMARK { stdr = LITERAL + stdstring; }
255  COMPARE(stdr, stdstring + stdstring);
256  }
257 
258 
259  void separator_3() { SEP("3 literals"); }
260 
261  void b_3_l1literal() {
262  QBENCHMARK { r = l1literal P l1literal P l1literal; }
263  COMPARE(r, r3);
264  }
265  void q_3_l1string() {
266  QBENCHMARK { r = l1string + l1string + l1string; }
267  COMPARE(r, r3);
268  }
269  void s_3_l1string() {
270  QBENCHMARK { stdr = stdstring + LITERAL + LITERAL; }
271  COMPARE(stdr, stdstring + stdstring + stdstring);
272  }
273 
274 
275  void separator_4() { SEP("4 literals"); }
276 
277  void b_4_l1literal() {
278  QBENCHMARK { r = l1literal P l1literal P l1literal P l1literal; }
279  COMPARE(r, r4);
280  }
281  void q_4_l1string() {
282  QBENCHMARK { r = l1string + l1string + l1string + l1string; }
283  COMPARE(r, r4);
284  }
285 
286 
287  void separator_5() { SEP("5 literals"); }
288 
289  void b_5_l1literal() {
290  QBENCHMARK { r = l1literal P l1literal P l1literal P l1literal P l1literal; }
291  COMPARE(r, r5);
292  }
293 
294  void q_5_l1string() {
295  QBENCHMARK { r = l1string + l1string + l1string + l1string + l1string; }
296  COMPARE(r, r5);
297  }
298 
299 
300  void separator_6() { SEP("4 chars"); }
301 
302  void b_string_4_char() {
303  QBENCHMARK { r = string + achar + achar + achar + achar; }
304  COMPARE(r, QString(string P achar P achar P achar P achar));
305  }
306 
307  void q_string_4_char() {
308  QBENCHMARK { r = string + achar + achar + achar + achar; }
309  COMPARE(r, QString(string P achar P achar P achar P achar));
310  }
311 
312  void s_string_4_char() {
313  QBENCHMARK { stdr = stdstring + 'c' + 'c' + 'c' + 'c'; }
314  COMPARE(stdr, stdstring + 'c' + 'c' + 'c' + 'c');
315  }
316 
317 
318  void separator_7() { SEP("char + string + char"); }
319 
320  void b_char_string_char() {
321  QBENCHMARK { r = achar + string + achar; }
322  COMPARE(r, QString(achar P string P achar));
323  }
324 
325  void q_char_string_char() {
326  QBENCHMARK { r = achar + string + achar; }
327  COMPARE(r, QString(achar P string P achar));
328  }
329 
330  void s_char_string_char() {
331  QBENCHMARK { stdr = 'c' + stdstring + 'c'; }
332  COMPARE(stdr, 'c' + stdstring + 'c');
333  }
334 
335 
336  void separator_8() { SEP("string.arg"); }
337 
338  void b_string_arg() {
339  const QString pattern = l1string + QString::fromLatin1("%1") + l1string;
340  QBENCHMARK { r = l1literal P string P l1literal; }
341  COMPARE(r, r3);
342  }
343 
344  void q_string_arg() {
345  const QString pattern = l1string + QLatin1String("%1") + l1string;
346  QBENCHMARK { r = pattern.arg(string); }
347  COMPARE(r, r3);
348  }
349 
350  void q_bytearray_arg() {
352  QBENCHMARK { result = ba + ba + ba; }
353  }
354 
355 
356  void separator_9() { SEP("QString::reserve()"); }
357 
358  void b_reserve() {
359  QBENCHMARK {
360  r.clear();
361  r = string P string P string P string;
362  }
363  COMPARE(r, r4);
364  }
365  void b_reserve_lit() {
366  QBENCHMARK {
367  r.clear();
368  r = string P l1literal P string P string;
369  }
370  COMPARE(r, r4);
371  }
372  void s_reserve() {
373  QBENCHMARK {
374  r.clear();
375  r.reserve(string.size() + string.size() + string.size() + string.size());
376  r += string;
377  r += string;
378  r += string;
379  r += string;
380  }
381  COMPARE(r, r4);
382  }
383  void s_reserve_lit() {
384  QBENCHMARK {
385  r.clear();
386  //r.reserve(string.size() + qstrlen(l1string.latin1())
387  // + string.size() + string.size());
388  r.reserve(1024);
389  r += string;
390  r += l1string;
391  r += string;
392  r += string;
393  }
394  COMPARE(r, r4);
395  }
396 
397 private:
398  const QLatin1String l1literal;
399  const QLatin1String l1string;
400  const QByteArray ba;
401  const QString string;
402  const std::string stdstring;
403  const QLatin1Char achar;
404  const QString r2, r3, r4, r5;
405 
406  // short cuts for results
407  QString r;
408  std::string stdr;
409 };
410 
411 #undef P
412 
414 
415 #include "tst_bench_qstringbuilder.moc"
small capitals from c petite p scientific i
[1]
Definition: afcover.h:80
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:85
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal.
Definition: qstring.h:84
The QObject class is the base class of all Qt objects.
Definition: qobject.h:125
The QString class provides a Unicode character string.
Definition: qstring.h:388
static QString fromLatin1(QByteArrayView ba)
Definition: qstring.cpp:5488
#define QString()
Definition: parse-defines.h:51
#define QBENCHMARK
Definition: qbenchmark.h:76
#define qDebug
[1]
Definition: qlogging.h:177
GLboolean r
[2]
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint64EXT * result
[6]
Definition: qopenglext.h:10932
GLdouble s
[6]
Definition: qopenglext.h:235
GLubyte * pattern
Definition: qopenglext.h:2744
GLsizei const GLchar *const * string
[0]
Definition: qopenglext.h:694
#define QTEST_MAIN(TestObject)
Definition: qtest.h:664
#define Q_OBJECT
Definition: qtmetamacros.h:158
#define slots
Definition: qtmetamacros.h:76
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:53
#define LITERAL
#define COMPARE(a, b)
#define SEP(s)