1 /////////////////////////////////////////////////////////////////////////////
2 // Name: tests/benchmarks/strings.cpp
3 // Purpose: String-related benchmarks
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows license
9 /////////////////////////////////////////////////////////////////////////////
11 #include "wx/string.h"
15 #include "htmlparser/htmlpars.h"
17 static const char asciistr
[] =
18 "This is just the first line of a very long 7 bit ASCII string"
19 "This is just the second line of a very long 7 bit ASCII string"
20 "This is just the third line of a very long 7 bit ASCII string"
21 "This is just the fourth line of a very long 7 bit ASCII string"
22 "This is just the fifth line of a very long 7 bit ASCII string"
23 "This is just the sixth line of a very long 7 bit ASCII string"
24 "This is just the seventh line of a very long 7 bit ASCII string"
25 "This is just the eighth line of a very long 7 bit ASCII string"
26 "This is just the ninth line of a very long 7 bit ASCII string"
27 "This is just the tenth line of a very long 7 bit ASCII string"
30 static const char utf8str
[] =
31 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 0"
32 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 1"
33 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 2"
34 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 3"
35 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 4"
36 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 5"
37 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 6"
38 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 7"
39 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 8"
40 "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 9"
43 // this is just a baseline
44 BENCHMARK_FUNC(Strlen
)
46 if ( strlen(utf8str
) != WXSIZEOF(utf8str
) - 1 )
49 if ( strlen(asciistr
) != WXSIZEOF(asciistr
) - 1 )
55 // ----------------------------------------------------------------------------
56 // FromUTF8() benchmarks
57 // ----------------------------------------------------------------------------
59 BENCHMARK_FUNC(FromUTF8
)
61 wxString s
= wxString::FromUTF8(utf8str
);
65 s
= wxString::FromUTF8(asciistr
);
72 BENCHMARK_FUNC(FromUTF8WithNpos
)
74 wxString s
= wxString::FromUTF8(utf8str
, wxString::npos
);
78 s
= wxString::FromUTF8(asciistr
, wxString::npos
);
85 BENCHMARK_FUNC(FromUTF8WithLen
)
87 wxString s
= wxString::FromUTF8(utf8str
, WXSIZEOF(utf8str
));
91 s
= wxString::FromUTF8(asciistr
, WXSIZEOF(asciistr
));
98 // ----------------------------------------------------------------------------
99 // FromUTF8Unchecked() benchmarks
100 // ----------------------------------------------------------------------------
102 BENCHMARK_FUNC(FromUTF8Unchecked
)
104 wxString s
= wxString::FromUTF8Unchecked(utf8str
);
108 s
= wxString::FromUTF8Unchecked(asciistr
);
115 BENCHMARK_FUNC(FromUTF8UncheckedWithNpos
)
117 wxString s
= wxString::FromUTF8Unchecked(utf8str
, wxString::npos
);
121 s
= wxString::FromUTF8Unchecked(asciistr
, wxString::npos
);
128 BENCHMARK_FUNC(FromUTF8UncheckedWithLen
)
130 wxString s
= wxString::FromUTF8Unchecked(utf8str
, WXSIZEOF(utf8str
));
134 s
= wxString::FromUTF8Unchecked(asciistr
, WXSIZEOF(asciistr
));
141 // ----------------------------------------------------------------------------
142 // FromAscii() benchmarks
143 // ----------------------------------------------------------------------------
145 BENCHMARK_FUNC(FromAscii
)
147 wxString s
= wxString::FromAscii(asciistr
);
154 BENCHMARK_FUNC(FromAsciiWithNpos
)
156 wxString s
= wxString::FromAscii(asciistr
);
163 BENCHMARK_FUNC(FromAsciiWithLen
)
165 wxString s
= wxString::FromAscii(asciistr
, WXSIZEOF(asciistr
));
172 // ----------------------------------------------------------------------------
173 // simple string iteration
174 // ----------------------------------------------------------------------------
177 BENCHMARK_FUNC(ForCString
)
179 for ( size_t n
= 0; n
< WXSIZEOF(asciistr
); n
++ )
181 if ( asciistr
[n
] == '~' )
188 BENCHMARK_FUNC(ForStringIndex
)
190 const wxString s
= wxString::FromAscii(asciistr
);
191 const size_t len
= s
.length();
192 for ( size_t n
= 0; n
< len
; n
++ )
201 BENCHMARK_FUNC(ForStringIter
)
203 const wxString s
= wxString::FromAscii(asciistr
);
204 const wxString::const_iterator end
= s
.end();
205 for ( wxString::const_iterator i
= s
.begin(); i
!= end
; ++i
)
214 BENCHMARK_FUNC(ForStringRIter
)
216 const wxString s
= wxString::FromAscii(asciistr
);
217 const wxString::const_reverse_iterator rend
= s
.rend();
218 for ( wxString::const_reverse_iterator i
= s
.rbegin(); i
!= rend
; ++i
)
227 // ----------------------------------------------------------------------------
228 // wxString::Replace()
229 // ----------------------------------------------------------------------------
231 const size_t ASCIISTR_LEN
= strlen(asciistr
);
233 BENCHMARK_FUNC(ReplaceLoop
)
235 wxString
str('x', ASCIISTR_LEN
);
236 for ( size_t n
= 0; n
< ASCIISTR_LEN
; n
++ )
242 return str
.length() != 0;
245 BENCHMARK_FUNC(ReplaceNone
)
247 wxString
str('x', ASCIISTR_LEN
);
248 return str
.Replace("a", "z") == 0;
251 BENCHMARK_FUNC(ReplaceSome
)
253 wxString
str(asciistr
);
254 return str
.Replace("7", "8") != 0;
257 BENCHMARK_FUNC(ReplaceAll
)
259 wxString
str('x', ASCIISTR_LEN
);
260 return str
.Replace("x", "y") != 0;
264 // ----------------------------------------------------------------------------
265 // string buffers: wx[W]CharBuffer
266 // ----------------------------------------------------------------------------
268 BENCHMARK_FUNC(CharBuffer
)
270 wxString
str(asciistr
);
272 // NB: wxStrlen() is here to simulate some use of the returned buffer.
273 // Both mb_str() and wc_str() are used so that this code does something
274 // nontrivial in any build.
275 return wxStrlen(str
.mb_str()) == ASCIISTR_LEN
&&
276 wxStrlen(str
.wc_str()) == ASCIISTR_LEN
;
280 // ----------------------------------------------------------------------------
281 // wxString::operator[] - parse large HTML page
282 // ----------------------------------------------------------------------------
284 class DummyParser
: public wx28HtmlParser
287 virtual wxObject
* GetProduct() { return NULL
; }
288 virtual void AddText(const wxChar
*) {}
292 BENCHMARK_FUNC(ParseHTML
)
294 // static so that construction time is not counted
295 static DummyParser parser
;
296 static wxString html
;
299 wxFFile("htmltest.html").ReadAll(&html
, wxConvUTF8
);