[Bug libstdc++/29037] New: performance problem with std::string operator=(const std::string& str);

neundorf at kde dot org gcc-bugzilla@gcc.gnu.org
Tue Sep 12 16:02:00 GMT 2006


Hi,

I tested the following behaviour with all gcc versions I had available (gcc
3.3.6 on x86 Slackware Linux, gcc 4.0.2 on x86 SUSE Linux, gcc 4.0.1 on OS X).
The following code:

std::string s1;
// in the following call memory is allocated
// and the capacity of s1 goes to 1024
s1.resize(1024); 
std::string s2;
s2="abc";
// in the following call the allocated memory is freed
// so that after the assignment s1 has the same capacity as s2
s1=s2;

While this follows the standard, which says that after the assignment
s1.capacity() >= s2. capacity() must be true, it can be quite a performance hit
when doing a lot of assignments.
With gcc the following is much faster in these cases:
s1=s2.c_str();
since then the capacity stays high and the dynamic memory management is
avoided.

Alex


-- 
           Summary: performance problem with std::string operator=(const
                    std::string& str);
           Product: gcc
           Version: 4.0.2
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: neundorf at kde dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29037



More information about the Gcc-bugs mailing list