X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/0654c03aff48601f9d18b05a6aabec45aa9bb4c9..ce00f59b5b169752d2f05ce3bb1a88ddc1b38b4c:/tests/benchmarks/strings.cpp

diff --git a/tests/benchmarks/strings.cpp b/tests/benchmarks/strings.cpp
index 1fc1b7d38e..616e371edf 100644
--- a/tests/benchmarks/strings.cpp
+++ b/tests/benchmarks/strings.cpp
@@ -5,12 +5,14 @@
 // Created:     2008-07-19
 // RCS-ID:      $Id$
 // Copyright:   (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
-// Licence:     wxWindows license
+// Licence:     wxWindows licence
 /////////////////////////////////////////////////////////////////////////////
 
 #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(asciistr);
+    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(asciistr);
+    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(asciistr);
+    const wxString& s = GetTestAsciiString();
     const wxString::const_reverse_iterator rend = s.rend();
     for ( wxString::const_reverse_iterator i = s.rbegin(); i != rend; ++i )
     {
@@ -258,6 +281,126 @@ BENCHMARK_FUNC(ReplaceAll)
     return str.Replace("x", "y") != 0;
 }
 
+BENCHMARK_FUNC(ReplaceLonger)
+{
+    wxString str('x', ASCIISTR_LEN);
+    return str.Replace("x", "yy") != 0;
+}
+
+BENCHMARK_FUNC(ReplaceShorter)
+{
+    wxString str('x', ASCIISTR_LEN);
+    return str.Replace("xx", "y") != 0;
+}
+
+// ----------------------------------------------------------------------------
+// string case conversion
+// ----------------------------------------------------------------------------
+
+BENCHMARK_FUNC(Lower)
+{
+    return GetTestAsciiString().Lower().length() > 0;
+}
+
+BENCHMARK_FUNC(Upper)
+{
+    return GetTestAsciiString().Upper().length() > 0;
+}
+
+// ----------------------------------------------------------------------------
+// string comparison
+// ----------------------------------------------------------------------------
+
+BENCHMARK_FUNC(StrcmpA)
+{
+    const wxString& s = GetTestAsciiString();
+
+    return wxCRT_StrcmpA(s, s) == 0;
+}
+
+BENCHMARK_FUNC(StrcmpW)
+{
+    const wxString& s = GetTestAsciiString();
+
+    return wxCRT_StrcmpW(s, s) == 0;
+}
+
+BENCHMARK_FUNC(StricmpA)
+{
+    const wxString& s = GetTestAsciiString();
+
+    return wxCRT_StricmpA(s, s) == 0;
+}
+
+BENCHMARK_FUNC(StricmpW)
+{
+    const wxString& s = GetTestAsciiString();
+
+    return wxCRT_StricmpW(s, s) == 0;
+}
+
+BENCHMARK_FUNC(StringCmp)
+{
+    const wxString& s = GetTestAsciiString();
+
+    return s.Cmp(s) == 0;
+}
+
+BENCHMARK_FUNC(StringCmpNoCase)
+{
+    const wxString& s = GetTestAsciiString();
+
+    return s.CmpNoCase(s) == 0;
+}
+
+// Also benchmark various native functions under MSW. Surprisingly/annoyingly
+// they sometimes have vastly better performance than alternatives, especially
+// for case-sensitive comparison (see #10375).
+#ifdef __WXMSW__
+
+#include "wx/msw/wrapwin.h"
+
+BENCHMARK_FUNC(MSWlstrcmp)
+{
+    const wxString& s = GetTestAsciiString();
+
+    return lstrcmp(s.t_str(), s.t_str()) == 0;
+}
+
+BENCHMARK_FUNC(MSWlstrcmpi)
+{
+    const wxString& s = GetTestAsciiString();
+
+    return lstrcmpi(s.t_str(), s.t_str()) == 0;
+}
+
+BENCHMARK_FUNC(MSWCompareString)
+{
+    const wxString& s = GetTestAsciiString();
+
+    return ::CompareString
+             (
+                LOCALE_USER_DEFAULT,
+                0,
+                s.t_str(), s.length(),
+                s.t_str(), s.length()
+             ) == CSTR_EQUAL;
+}
+
+BENCHMARK_FUNC(MSWCompareStringIgnoreCase)
+{
+    const wxString& s = GetTestAsciiString();
+
+    return ::CompareString
+             (
+                LOCALE_USER_DEFAULT,
+                NORM_IGNORECASE,
+                s.t_str(), s.length(),
+                s.t_str(), s.length()
+             ) == CSTR_EQUAL;
+}
+
+#endif // __WXMSW__
 
 // ----------------------------------------------------------------------------
 // string buffers: wx[W]CharBuffer
@@ -273,3 +416,41 @@ BENCHMARK_FUNC(CharBuffer)
     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;
+}