4 ms·
What's interesting here to me is that C++ strings are not immutable. So I'd have expected them to behave basically the same way as StringBuilders in other langu
by Eiwatah4 13y ago
What's interesting here to me is that C++ strings are not immutable. So I'd have expected them to behave basically the same way as StringBuilders in other languages. But apparently they are required to be stored continuously, and I guess that's what makes them slower here.
- archangel_one 13y agoYes, I think the semantics of c_str() and data() effectively require that it is stored contiguously. Although it is still possible to make it faster by overallocating in the same way as std::vector, but at the cost of more memory use.
- Someone 13y agoI haven't looked at any implementation recently, but the standard specifically leaves open that implementatioms postpone joining string buffers until c_str() or data() is called (also, the pointers returned by those calls could contain copies of the strings; that is not something I would expect, but I see nothing in the standard that precludes it)
- Eiwatah4 13y agohttp://en.cppreference.com/w/cpp/string/basic_string/c_str http://en.cppreference.com/w/cpp/string/basic_string/c_str According to that link, c_str() and data() work in constant time. With that restriction, it's impossible to do the joining lazily - it must be done when data is added to the string.
- Someone 13y agoHa, it looks like they changed that in C++11. http://www.cplusplus.com/reference/string/string/data/ http://www.cplusplus.com/reference/string/string/data/ claims "Unspecified or contradictory specifications." for C++98, but constant complexity for C++11. An answer to http://programmers.stackexchange.com/questions/124731/what-performance-can-we-expect-from-stdstrings-c-str-always-constant-time http://programmers.stackexchange.com/questions/124731/what-p... indicates that C++03 doesn't require constnat time, either. Thanks for the education.