wxString is far slower than std::string (sometimes more than 20 times)

Armel Asselin armelasselin at hotmail.com
Wed Jan 17 02:50:58 PST 2007


> > > Even with a reserve or resize it seems that the data change address 
> > > all
> > > the time (but less often with std::string)
> > > Time wxString replace : 25343 OUPS !   Time std::string replace : 61
> > >
> > > Time wxString replace : 9741 OUPS !   Time std::string replace : 62
> > >
> > replace is O(n2) in wx implementation.
> >
> > Armel
> Maybe and what about std::string ? because the difference is quite big
> here
yes... basically wxString::replace does something similar to:
    while (!find occurrence in this)
        replace this by concat of 'before occurrence + replacement text + 
after occurrence'

whereas fast implementation do something like
    find all occurrences, [the problem here is how to store that 
efficiently, and generally WITHOUT new/delete, at least for small amounts]
    allocate enough space for all text between occurrences and for 
replacements
    concat all

BTW, the longest the strings and the number of replacements 
wxString::replace is O(mn) in fact m: number of replacements, n length of 
string, this is why replacing : 'a' by 'b' in 'aaaaaaaaaaaaaaaaaaaaa' will 
be much slower than in '---------a----------' (of similar length).
also, it had a big impact on wxFileSystem stuff when i last checked, because 
the function replacing '/' with '\' (or the contrary) was based on replace.

Armel








More information about the wx-users mailing list