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 static 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"
28 static 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"
41 // this is just a baseline
42 BENCHMARK_FUNC(Strlen
)
44 if ( strlen(utf8str
) != WXSIZEOF(utf8str
) - 1 )
47 if ( strlen(asciistr
) != WXSIZEOF(asciistr
) - 1 )
53 // ----------------------------------------------------------------------------
54 // FromUTF8() benchmarks
55 // ----------------------------------------------------------------------------
57 BENCHMARK_FUNC(FromUTF8
)
59 wxString s
= wxString::FromUTF8(utf8str
);
63 s
= wxString::FromUTF8(asciistr
);
70 BENCHMARK_FUNC(FromUTF8WithNpos
)
72 wxString s
= wxString::FromUTF8(utf8str
, wxString::npos
);
76 s
= wxString::FromUTF8(asciistr
, wxString::npos
);
83 BENCHMARK_FUNC(FromUTF8WithLen
)
85 wxString s
= wxString::FromUTF8(utf8str
, WXSIZEOF(utf8str
));
89 s
= wxString::FromUTF8(asciistr
, WXSIZEOF(asciistr
));
96 // ----------------------------------------------------------------------------
97 // FromUTF8Unchecked() benchmarks
98 // ----------------------------------------------------------------------------
100 BENCHMARK_FUNC(FromUTF8Unchecked
)
102 wxString s
= wxString::FromUTF8Unchecked(utf8str
);
106 s
= wxString::FromUTF8Unchecked(asciistr
);
113 BENCHMARK_FUNC(FromUTF8UncheckedWithNpos
)
115 wxString s
= wxString::FromUTF8Unchecked(utf8str
, wxString::npos
);
119 s
= wxString::FromUTF8Unchecked(asciistr
, wxString::npos
);
126 BENCHMARK_FUNC(FromUTF8UncheckedWithLen
)
128 wxString s
= wxString::FromUTF8Unchecked(utf8str
, WXSIZEOF(utf8str
));
132 s
= wxString::FromUTF8Unchecked(asciistr
, WXSIZEOF(asciistr
));
139 // ----------------------------------------------------------------------------
140 // FromAscii() benchmarks
141 // ----------------------------------------------------------------------------
143 BENCHMARK_FUNC(FromAscii
)
145 wxString s
= wxString::FromAscii(asciistr
);
152 BENCHMARK_FUNC(FromAsciiWithNpos
)
154 wxString s
= wxString::FromAscii(asciistr
);
161 BENCHMARK_FUNC(FromAsciiWithLen
)
163 wxString s
= wxString::FromAscii(asciistr
, WXSIZEOF(asciistr
));
170 // ----------------------------------------------------------------------------
171 // simple string iteration
172 // ----------------------------------------------------------------------------
175 BENCHMARK_FUNC(ForCString
)
177 for ( size_t n
= 0; n
< WXSIZEOF(asciistr
); n
++ )
179 if ( asciistr
[n
] == '~' )
186 BENCHMARK_FUNC(ForStringIndex
)
188 const wxString s
= wxString::FromAscii(utf8str
);
189 const size_t len
= s
.length();
190 for ( size_t n
= 0; n
< len
; n
++ )
199 BENCHMARK_FUNC(ForStringIter
)
201 const wxString s
= wxString::FromAscii(utf8str
);
202 const wxString::const_iterator end
= s
.end();
203 for ( wxString::const_iterator i
= s
.begin(); i
!= end
; ++i
)
212 BENCHMARK_FUNC(ForStringRIter
)
214 const wxString s
= wxString::FromAscii(utf8str
);
215 const wxString::const_reverse_iterator rend
= s
.rend();
216 for ( wxString::const_reverse_iterator i
= s
.rbegin(); i
!= rend
; ++i
)