]> git.saurik.com Git - wxWidgets.git/blame - tests/benchmarks/strings.cpp
HAVE_BOOST_THREAD should be off by default
[wxWidgets.git] / tests / benchmarks / strings.cpp
CommitLineData
dc2ae355
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: tests/benchmarks/strings.cpp
3// Purpose: String-related benchmarks
4// Author: Vadim Zeitlin
5// Created: 2008-07-19
6// RCS-ID: $Id$
7// Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8// Licence: wxWindows license
9/////////////////////////////////////////////////////////////////////////////
10
11#include "wx/string.h"
12
13#include "bench.h"
14
15static const char asciistr[] =
16 "This is just the first line of a very long 7 bit ASCII string"
17 "This is just the second line of a very long 7 bit ASCII string"
18 "This is just the third line of a very long 7 bit ASCII string"
19 "This is just the fourth line of a very long 7 bit ASCII string"
20 "This is just the fifth line of a very long 7 bit ASCII string"
21 "This is just the sixth line of a very long 7 bit ASCII string"
22 "This is just the seventh line of a very long 7 bit ASCII string"
23 "This is just the eighth line of a very long 7 bit ASCII string"
24 "This is just the ninth line of a very long 7 bit ASCII string"
25 "This is just the tenth line of a very long 7 bit ASCII string"
26 ;
27
28static const char utf8str[] =
29 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 0"
30 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 1"
31 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 2"
32 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 3"
33 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 4"
34 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 5"
35 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 6"
36 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 7"
37 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 8"
38 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 9"
39 ;
40
41// this is just a baseline
42BENCHMARK_FUNC(Strlen)
43{
44 if ( strlen(utf8str) != WXSIZEOF(utf8str) - 1 )
45 return false;
46
47 if ( strlen(asciistr) != WXSIZEOF(asciistr) - 1 )
48 return false;
49
50 return true;
51}
52
53// ----------------------------------------------------------------------------
54// FromUTF8() benchmarks
55// ----------------------------------------------------------------------------
56
57BENCHMARK_FUNC(FromUTF8)
58{
59 wxString s = wxString::FromUTF8(utf8str);
60 if ( s.empty() )
61 return false;
62
63 s = wxString::FromUTF8(asciistr);
64 if ( s.empty() )
65 return false;
66
67 return true;
68}
69
70BENCHMARK_FUNC(FromUTF8WithNpos)
71{
72 wxString s = wxString::FromUTF8(utf8str, wxString::npos);
73 if ( s.empty() )
74 return false;
75
76 s = wxString::FromUTF8(asciistr, wxString::npos);
77 if ( s.empty() )
78 return false;
79
80 return true;
81}
82
83BENCHMARK_FUNC(FromUTF8WithLen)
84{
85 wxString s = wxString::FromUTF8(utf8str, WXSIZEOF(utf8str));
86 if ( s.empty() )
87 return false;
88
89 s = wxString::FromUTF8(asciistr, WXSIZEOF(asciistr));
90 if ( s.empty() )
91 return false;
92
93 return true;
94}
95
96// ----------------------------------------------------------------------------
97// FromUTF8Unchecked() benchmarks
98// ----------------------------------------------------------------------------
99
100BENCHMARK_FUNC(FromUTF8Unchecked)
101{
102 wxString s = wxString::FromUTF8Unchecked(utf8str);
103 if ( s.empty() )
104 return false;
105
106 s = wxString::FromUTF8Unchecked(asciistr);
107 if ( s.empty() )
108 return false;
109
110 return true;
111}
112
113BENCHMARK_FUNC(FromUTF8UncheckedWithNpos)
114{
115 wxString s = wxString::FromUTF8Unchecked(utf8str, wxString::npos);
116 if ( s.empty() )
117 return false;
118
119 s = wxString::FromUTF8Unchecked(asciistr, wxString::npos);
120 if ( s.empty() )
121 return false;
122
123 return true;
124}
125
126BENCHMARK_FUNC(FromUTF8UncheckedWithLen)
127{
128 wxString s = wxString::FromUTF8Unchecked(utf8str, WXSIZEOF(utf8str));
129 if ( s.empty() )
130 return false;
131
132 s = wxString::FromUTF8Unchecked(asciistr, WXSIZEOF(asciistr));
133 if ( s.empty() )
134 return false;
135
136 return true;
137}
138
139// ----------------------------------------------------------------------------
140// FromAscii() benchmarks
141// ----------------------------------------------------------------------------
142
143BENCHMARK_FUNC(FromAscii)
144{
145 wxString s = wxString::FromAscii(asciistr);
146 if ( s.empty() )
147 return false;
148
149 return true;
150}
151
152BENCHMARK_FUNC(FromAsciiWithNpos)
153{
154 wxString s = wxString::FromAscii(asciistr);
155 if ( s.empty() )
156 return false;
157
158 return true;
159}
160
161BENCHMARK_FUNC(FromAsciiWithLen)
162{
163 wxString s = wxString::FromAscii(asciistr, WXSIZEOF(asciistr));
164 if ( s.empty() )
165 return false;
166
167 return true;
168}
169
170// ----------------------------------------------------------------------------
171// simple string iteration
172// ----------------------------------------------------------------------------
173
174// baseline
175BENCHMARK_FUNC(ForCString)
176{
177 for ( size_t n = 0; n < WXSIZEOF(asciistr); n++ )
178 {
179 if ( asciistr[n] == '~' )
180 return false;
181 }
182
183 return true;
184}
185
186BENCHMARK_FUNC(ForStringIndex)
187{
ac41e143 188 const wxString s = wxString::FromAscii(asciistr);
dc2ae355
VZ
189 const size_t len = s.length();
190 for ( size_t n = 0; n < len; n++ )
191 {
192 if ( s[n] == '~' )
193 return false;
194 }
195
196 return true;
197}
198
199BENCHMARK_FUNC(ForStringIter)
200{
ac41e143 201 const wxString s = wxString::FromAscii(asciistr);
dc2ae355
VZ
202 const wxString::const_iterator end = s.end();
203 for ( wxString::const_iterator i = s.begin(); i != end; ++i )
204 {
205 if ( *i == '~' )
206 return false;
207 }
208
209 return true;
210}
211
212BENCHMARK_FUNC(ForStringRIter)
213{
ac41e143 214 const wxString s = wxString::FromAscii(asciistr);
dc2ae355
VZ
215 const wxString::const_reverse_iterator rend = s.rend();
216 for ( wxString::const_reverse_iterator i = s.rbegin(); i != rend; ++i )
217 {
218 if ( *i == '~' )
219 return false;
220 }
221
222 return true;
223}
224
9729777e
VZ
225// ----------------------------------------------------------------------------
226// wxString::Replace()
227// ----------------------------------------------------------------------------
228
0654c03a 229const size_t ASCIISTR_LEN = strlen(asciistr);
9729777e
VZ
230
231BENCHMARK_FUNC(ReplaceLoop)
232{
0654c03a
VS
233 wxString str('x', ASCIISTR_LEN);
234 for ( size_t n = 0; n < ASCIISTR_LEN; n++ )
9729777e
VZ
235 {
236 if ( str[n] == 'a' )
237 str[n] = 'z';
238 }
239
240 return str.length() != 0;
241}
242
543cf9ba 243BENCHMARK_FUNC(ReplaceNone)
9729777e 244{
0654c03a 245 wxString str('x', ASCIISTR_LEN);
543cf9ba
VZ
246 return str.Replace("a", "z") == 0;
247}
9729777e 248
543cf9ba
VZ
249BENCHMARK_FUNC(ReplaceSome)
250{
251 wxString str(asciistr);
252 return str.Replace("7", "8") != 0;
9729777e
VZ
253}
254
543cf9ba 255BENCHMARK_FUNC(ReplaceAll)
9729777e 256{
0654c03a 257 wxString str('x', ASCIISTR_LEN);
543cf9ba 258 return str.Replace("x", "y") != 0;
9729777e
VZ
259}
260
0654c03a
VS
261
262// ----------------------------------------------------------------------------
263// string buffers: wx[W]CharBuffer
264// ----------------------------------------------------------------------------
265
266BENCHMARK_FUNC(CharBuffer)
267{
268 wxString str(asciistr);
269
270 // NB: wxStrlen() is here to simulate some use of the returned buffer.
271 // Both mb_str() and wc_str() are used so that this code does something
272 // nontrivial in any build.
273 return wxStrlen(str.mb_str()) == ASCIISTR_LEN &&
274 wxStrlen(str.wc_str()) == ASCIISTR_LEN;
275}