std::string is not reference counted

After Visual C++ 6.0, Microsoft has changed the implementation of std::string to non-reference counted object due to threading issues.

http://support.microsoft.com/kb/813810

If your in-loop functions take/return std::string object by value, it could be a cause of performance bottle neck.

Here is the actual performance taken on my laptop with VisualC++ 8.0 runtime. It copies various length of  string objects to another std::string variable.

 

int main(){
    for(size_t len = 1; len < 1024 * 1024 * 100; len += 1024 * 1024){
        string str1;
        for(size_t i = 0; i < len; i++){
            str1 += "a";
        }
        boost::timer tim;
        string str2 = str1;
        cout << int(len / 1024 / 1024) << " " << tim.elapsed() << endl;
    }
   
    return 0;
}

  6a00d10a7a8c668bfa00d414424cc06a47

A stack trace when memcpy happens is:

ChildEBP RetAddr 
0012fe04 781803a5 MSVCR80!memcpy
0012fe20 7c421201 MSVCR80!memcpy_s+0x48
0012fe38 7c42394d MSVCP80!std::char_traits<char>::_Copy_s+0x15
0012fe5c 7c4248f9 MSVCP80!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign+0x7e
0012fe70 00401357 MSVCP80!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::basic_string<char,std::char_traits<char>,std::allocator<char> >+0x20
0012ff7c 0040157f string_copy!main+0x97
0012ffc0 7c816fd7 string_copy!__tmainCRTStartup+0x10f
0012fff0 00000000 kernel32!BaseProcessStart+0x23

Advertisements

About Moto

Engineer who likes coding
This entry was posted in C++. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s