News for hackers. Stuff that matters.
Re:Performance Improvement with the StringBuilder for C++ by dalore on Friday September 06, 2013 @10:28AM (#6340578)
This isn't just a C++ issue, in nearly every language strings will be immutable, that is if you add strings together it needs to create a new string somewhere with the new length of the string. So if you adding multiple strings together it does this each time. The better way (and how stringbuilder and it's elk do it) is to put the strings into an array and then concat (or even better output/send that to the thing that is needing the concatted string).
Re:Performance Improvement with the StringBuilder for C++ by js2 on Friday September 06, 2013 @10:41AM (#6340654)
Modern Python is usually smart enough to optimize:

  s += "..."
Though I'm not sure if it similarly optimizes:

  s = s + "..."
But old habits die hard so I still use a list of strings and join them at the end.
Re:Performance Improvement with the StringBuilder for C++ by Eiwatah4 on Friday September 06, 2013 @10:55AM (#6340735)
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.
Re:Performance Improvement with the StringBuilder for C++ by archangel_one on Friday September 06, 2013 @12:08PM (#6341117)
Yes, 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.

Re:Performance Improvement with the StringBuilder for C++ by Someone on Friday September 06, 2013 @02:52PM (#6342010)
I 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)
Re:Performance Improvement with the StringBuilder for C++ by Eiwatah4 on Friday September 06, 2013 @03:43PM (#6342239)
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.

Re:Performance Improvement with the StringBuilder for C++ by Someone on Friday September 06, 2013 @05:03PM (#6342668)
Ha, it looks like they changed that in C++11. 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-p... indicates that C++03 doesn't require constnat time, either.

Thanks for the education.