X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9729777e2af04af8d16c60bc7762272951c6bf12..0576cd9ebe3430bdf7acf69e1dd1df92273f32ad:/tests/benchmarks/strings.cpp diff --git a/tests/benchmarks/strings.cpp b/tests/benchmarks/strings.cpp index 321a860f4a..9449f49110 100644 --- a/tests/benchmarks/strings.cpp +++ b/tests/benchmarks/strings.cpp @@ -9,8 +9,10 @@ ///////////////////////////////////////////////////////////////////////////// #include "wx/string.h" +#include "wx/ffile.h" #include "bench.h" +#include "htmlparser/htmlpars.h" static const char asciistr[] = "This is just the first line of a very long 7 bit ASCII string" @@ -38,6 +40,27 @@ static const char utf8str[] = "\xD0\xA6\xD0\xB5\xD0\xBB\xD0\xBE\xD0\xB5 \xD1\x87\xD0\xB8\xD1\x81\xD0\xBB\xD0\xBE 9" ; +namespace +{ + +const wxString& GetTestAsciiString() +{ + static wxString testString; + if ( testString.empty() ) + { + long num = Bench::GetNumericParameter(); + if ( !num ) + num = 1; + + for ( long n = 0; n < num; n++ ) + testString += wxString::FromAscii(asciistr); + } + + return testString; +} + +} // anonymous namespace + // this is just a baseline BENCHMARK_FUNC(Strlen) { @@ -185,7 +208,7 @@ BENCHMARK_FUNC(ForCString) BENCHMARK_FUNC(ForStringIndex) { - const wxString s = wxString::FromAscii(utf8str); + const wxString& s = GetTestAsciiString(); const size_t len = s.length(); for ( size_t n = 0; n < len; n++ ) { @@ -198,7 +221,7 @@ BENCHMARK_FUNC(ForStringIndex) BENCHMARK_FUNC(ForStringIter) { - const wxString s = wxString::FromAscii(utf8str); + const wxString& s = GetTestAsciiString(); const wxString::const_iterator end = s.end(); for ( wxString::const_iterator i = s.begin(); i != end; ++i ) { @@ -211,7 +234,7 @@ BENCHMARK_FUNC(ForStringIter) BENCHMARK_FUNC(ForStringRIter) { - const wxString s = wxString::FromAscii(utf8str); + const wxString& s = GetTestAsciiString(); const wxString::const_reverse_iterator rend = s.rend(); for ( wxString::const_reverse_iterator i = s.rbegin(); i != rend; ++i ) { @@ -226,12 +249,12 @@ BENCHMARK_FUNC(ForStringRIter) // wxString::Replace() // ---------------------------------------------------------------------------- -const size_t REPLACE_STR_LEN = 1000; +const size_t ASCIISTR_LEN = strlen(asciistr); BENCHMARK_FUNC(ReplaceLoop) { - wxString str('x', REPLACE_STR_LEN); - for ( size_t n = 0; n < REPLACE_STR_LEN; n++ ) + wxString str('x', ASCIISTR_LEN); + for ( size_t n = 0; n < ASCIISTR_LEN; n++ ) { if ( str[n] == 'a' ) str[n] = 'z'; @@ -240,19 +263,74 @@ BENCHMARK_FUNC(ReplaceLoop) return str.length() != 0; } -BENCHMARK_FUNC(ReplaceMiss) +BENCHMARK_FUNC(ReplaceNone) { - wxString str('x', REPLACE_STR_LEN); - str.Replace("a", "z"); + wxString str('x', ASCIISTR_LEN); + return str.Replace("a", "z") == 0; +} - return str.length() != 0; +BENCHMARK_FUNC(ReplaceSome) +{ + wxString str(asciistr); + return str.Replace("7", "8") != 0; } -BENCHMARK_FUNC(ReplaceHit) +BENCHMARK_FUNC(ReplaceAll) { - wxString str('x', REPLACE_STR_LEN); - str.Replace("x", "y"); + wxString str('x', ASCIISTR_LEN); + return str.Replace("x", "y") != 0; +} - return str.length() != 0; + +// ---------------------------------------------------------------------------- +// string buffers: wx[W]CharBuffer +// ---------------------------------------------------------------------------- + +BENCHMARK_FUNC(CharBuffer) +{ + wxString str(asciistr); + + // NB: wxStrlen() is here to simulate some use of the returned buffer. + // Both mb_str() and wc_str() are used so that this code does something + // nontrivial in any build. + return wxStrlen(str.mb_str()) == ASCIISTR_LEN && + wxStrlen(str.wc_str()) == ASCIISTR_LEN; } + +// ---------------------------------------------------------------------------- +// wxString::operator[] - parse large HTML page +// ---------------------------------------------------------------------------- + +class DummyParser : public wx28HtmlParser +{ +public: + virtual wxObject* GetProduct() { return NULL; } + virtual void AddText(const wxChar*) {} +}; + + +BENCHMARK_FUNC(ParseHTML) +{ + // static so that construction time is not counted + static DummyParser parser; + static wxString html; + if ( html.empty() ) + { + wxString html1; + wxFFile("htmltest.html").ReadAll(&html1, wxConvUTF8); + + // this is going to make for some invalid HTML, of course, but it + // doesn't really matter + long num = Bench::GetNumericParameter(); + if ( !num ) + num = 1; + + for ( long n = 0; n < num; n++ ) + html += html1; + } + + parser.Parse(html); + + return true; +}