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"
46 const wxString
& GetTestAsciiString()
48 static wxString testString
;
49 if ( testString
.empty() )
51 for ( long n
= 0; n
< Bench::GetNumericParameter(); n
++ )
52 testString
+= wxString::FromAscii(asciistr
);
58 } // anonymous namespace
60 // this is just a baseline
61 BENCHMARK_FUNC(Strlen
)
63 if ( strlen(utf8str
) != WXSIZEOF(utf8str
) - 1 )
66 if ( strlen(asciistr
) != WXSIZEOF(asciistr
) - 1 )
72 // ----------------------------------------------------------------------------
73 // FromUTF8() benchmarks
74 // ----------------------------------------------------------------------------
76 BENCHMARK_FUNC(FromUTF8
)
78 wxString s
= wxString::FromUTF8(utf8str
);
82 s
= wxString::FromUTF8(asciistr
);
89 BENCHMARK_FUNC(FromUTF8WithNpos
)
91 wxString s
= wxString::FromUTF8(utf8str
, wxString::npos
);
95 s
= wxString::FromUTF8(asciistr
, wxString::npos
);
102 BENCHMARK_FUNC(FromUTF8WithLen
)
104 wxString s
= wxString::FromUTF8(utf8str
, WXSIZEOF(utf8str
));
108 s
= wxString::FromUTF8(asciistr
, WXSIZEOF(asciistr
));
115 // ----------------------------------------------------------------------------
116 // FromUTF8Unchecked() benchmarks
117 // ----------------------------------------------------------------------------
119 BENCHMARK_FUNC(FromUTF8Unchecked
)
121 wxString s
= wxString::FromUTF8Unchecked(utf8str
);
125 s
= wxString::FromUTF8Unchecked(asciistr
);
132 BENCHMARK_FUNC(FromUTF8UncheckedWithNpos
)
134 wxString s
= wxString::FromUTF8Unchecked(utf8str
, wxString::npos
);
138 s
= wxString::FromUTF8Unchecked(asciistr
, wxString::npos
);
145 BENCHMARK_FUNC(FromUTF8UncheckedWithLen
)
147 wxString s
= wxString::FromUTF8Unchecked(utf8str
, WXSIZEOF(utf8str
));
151 s
= wxString::FromUTF8Unchecked(asciistr
, WXSIZEOF(asciistr
));
158 // ----------------------------------------------------------------------------
159 // FromAscii() benchmarks
160 // ----------------------------------------------------------------------------
162 BENCHMARK_FUNC(FromAscii
)
164 wxString s
= wxString::FromAscii(asciistr
);
171 BENCHMARK_FUNC(FromAsciiWithNpos
)
173 wxString s
= wxString::FromAscii(asciistr
);
180 BENCHMARK_FUNC(FromAsciiWithLen
)
182 wxString s
= wxString::FromAscii(asciistr
, WXSIZEOF(asciistr
));
189 // ----------------------------------------------------------------------------
190 // simple string iteration
191 // ----------------------------------------------------------------------------
194 BENCHMARK_FUNC(ForCString
)
196 for ( size_t n
= 0; n
< WXSIZEOF(asciistr
); n
++ )
198 if ( asciistr
[n
] == '~' )
205 BENCHMARK_FUNC(ForStringIndex
)
207 const wxString
& s
= GetTestAsciiString();
208 const size_t len
= s
.length();
209 for ( size_t n
= 0; n
< len
; n
++ )
218 BENCHMARK_FUNC(ForStringIter
)
220 const wxString
& s
= GetTestAsciiString();
221 const wxString::const_iterator end
= s
.end();
222 for ( wxString::const_iterator i
= s
.begin(); i
!= end
; ++i
)
231 BENCHMARK_FUNC(ForStringRIter
)
233 const wxString
& s
= GetTestAsciiString();
234 const wxString::const_reverse_iterator rend
= s
.rend();
235 for ( wxString::const_reverse_iterator i
= s
.rbegin(); i
!= rend
; ++i
)
244 // ----------------------------------------------------------------------------
245 // wxString::Replace()
246 // ----------------------------------------------------------------------------
248 const size_t ASCIISTR_LEN
= strlen(asciistr
);
250 BENCHMARK_FUNC(ReplaceLoop
)
252 wxString
str('x', ASCIISTR_LEN
);
253 for ( size_t n
= 0; n
< ASCIISTR_LEN
; n
++ )
259 return str
.length() != 0;
262 BENCHMARK_FUNC(ReplaceNone
)
264 wxString
str('x', ASCIISTR_LEN
);
265 return str
.Replace("a", "z") == 0;
268 BENCHMARK_FUNC(ReplaceSome
)
270 wxString
str(asciistr
);
271 return str
.Replace("7", "8") != 0;
274 BENCHMARK_FUNC(ReplaceAll
)
276 wxString
str('x', ASCIISTR_LEN
);
277 return str
.Replace("x", "y") != 0;
281 // ----------------------------------------------------------------------------
282 // string buffers: wx[W]CharBuffer
283 // ----------------------------------------------------------------------------
285 BENCHMARK_FUNC(CharBuffer
)
287 wxString
str(asciistr
);
289 // NB: wxStrlen() is here to simulate some use of the returned buffer.
290 // Both mb_str() and wc_str() are used so that this code does something
291 // nontrivial in any build.
292 return wxStrlen(str
.mb_str()) == ASCIISTR_LEN
&&
293 wxStrlen(str
.wc_str()) == ASCIISTR_LEN
;
297 // ----------------------------------------------------------------------------
298 // wxString::operator[] - parse large HTML page
299 // ----------------------------------------------------------------------------
301 class DummyParser
: public wx28HtmlParser
304 virtual wxObject
* GetProduct() { return NULL
; }
305 virtual void AddText(const wxChar
*) {}
309 BENCHMARK_FUNC(ParseHTML
)
311 // static so that construction time is not counted
312 static DummyParser parser
;
313 static wxString html
;
317 wxFFile("htmltest.html").ReadAll(&html1
, wxConvUTF8
);
319 // this is going to make for some invalid HTML, of course, but it
320 // doesn't really matter
321 for ( long n
= 0; n
< Bench::GetNumericParameter(); n
++ )