Which code is faster

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 427
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Which code is faster

Post by cutecode »

Hello, I wonder which code is faster

this?

Code: Select all

wxString sz1 = L"first";
wxString sz2 = L"second";

if(wcscmp(sz1, sz2) == 0)
	dosomething();
or this?

Code: Select all

wxString sz1 = L"first";
wxString sz2 = L"second";

if(sz1 == sz2)
	dosomething();
In my code I use very often string comparing and I want to ask you, how should I rewrite my code?
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Which code is faster

Post by doublemax »

I wouldn't expect any measurable difference between the two. The wxString version has the small advantage that it stores the length of the string, and if the length of the two strings is different, the comparison will immediately return false.

I'd suggest to write a small benchmark yourself and try it out.
Use the source, Luke!
User avatar
cutecode
Super wx Problem Solver
Super wx Problem Solver
Posts: 427
Joined: Fri Dec 09, 2016 7:28 am
Contact:

Re: Which code is faster

Post by cutecode »

doublemax wrote: Tue Sep 18, 2018 1:17 pm I'd suggest to write a small benchmark yourself and try it out.
I tried to make a benchmark and I got strange results.
in debug mode == is faster 2 times then wcscmp
but in release mode wcscmp and == are almost equel

if lenngths of strings are different, then == is almost 10 times faster

Code: Select all

bool dosomething()
{
    return 0;
}

bool soft_itUApp::OnInit()
{
    const int mx = 10000000000;
    wxString z1 = "xxxx";
    wxString z2 = "aaaa";
    wxLongLong l0, l2, ll1, ll2, ll3;

    l0 = wxDateTime::Now().GetTicks();
    for(UINT i = 0; i< mx; i++)
    {
        if(wcscmp(z1, z2))
            dosomething();
    }
    l2 = wxDateTime::Now().GetTicks();
    ll1 = l2 - l0;

    l0 = wxDateTime::Now().GetTicks();
    for(UINT i = 0; i< mx; i++)
    {
        if(z1.IsSameAs(z2))
            dosomething();
    }
    l2 = wxDateTime::Now().GetTicks();

    ll2 = l2 - l0;

    l0 = wxDateTime::Now().GetTicks();
    for(UINT i = 0; i< mx; i++)
    {
        if(z1 == z2)
            dosomething();
    }
    l2 = wxDateTime::Now().GetTicks();

    ll3 = l2 - l0;
    wxMessageBox(ll1.ToString() + L":" + ll2.ToString() + L":" + ll3.ToString());
tested on debian 9 with i7 proc
wx 3.1.6 win/mac/linux

regards,
Alexander Saprykin
https://v2.dental-soft.ru
User avatar
doublemax
Moderator
Moderator
Posts: 19159
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Which code is faster

Post by doublemax »

if lengths of strings are different, then == is almost 10 times faster
This makes sense as "==" only tests for equality and if the strings have different lengths, they can't be equal. wcscmp needs to compare the string data even in this case, because the return value indicates which one is "bigger".

BTW: wxStopWatch is a useful class for benchmarks.
https://docs.wxwidgets.org/trunk/classw ... watch.html
Use the source, Luke!
Post Reply