Re:Performance Improvement with the StringBuilder for C++
by quchen on Friday September 06, 2013 @10:27AM
(#6340576)
Why concatenate the strings at all instead of printing them piece by piece? Don't flush in between and it'll land in the same buffer. (Even if you flushed after every string, if output is "lightning fast", that shouldn't matter either.)
[ Parent
| on HN ]
Re:Performance Improvement with the StringBuilder for C++
by alayne on Friday September 06, 2013 @10:31AM
(#6340603)
It says they are to be written to a file. That's like the textbook case for buffered I/O writes. Get rid of that silly concatenation, it's just increasing the amount of data copying.
Re:Performance Improvement with the StringBuilder for C++
by gngeal on Friday September 06, 2013 @10:37AM
(#6340637)
String concatenation? Are we living in the 1980's? What about string trees? These problems can be solved simply using a proper data structure. Concatenation, insertion, deletion etc. should be almost constant-time operations.
Re:Performance Improvement with the StringBuilder for C++
by alayne on Friday September 06, 2013 @10:39AM
(#6340643)
Data structures like ropes are really more for editing than concatenation.
Re:Performance Improvement with the StringBuilder for C++
by phaemon on Friday September 06, 2013 @11:29AM
(#6340897)
I'd never heard of "ropes" in that context and read that as the worst simile ever ("Data structures, like ropes, are...")
Re:Performance Improvement with the StringBuilder for C++
by lucian1900 on Friday September 06, 2013 @11:26AM
(#6340883)
Trees (like the ropes you might use in this case) are not necessarily faster on modern CPUs. Caches and branch prediction tend to dominate performance in such cases.
Re:Performance Improvement with the StringBuilder for C++
by gngeal on Friday September 06, 2013 @02:49PM
(#6341997)
I don't see how branch prediction and caches could dominate performance of logically concatenating a huge amount of text instead of doing it physically.
Re:Performance Improvement with the StringBuilder for C++
by dkersten on Friday September 06, 2013 @03:16PM
(#6342119)
I guess it depends on what other operations you need. If the only operation you need is concatenation then trees are definitely faster. Otherwise it depends.
Re:Performance Improvement with the StringBuilder for C++
by lucian1900 on Saturday September 07, 2013 @01:45PM
(#6345789)
Arrays and copies are often faster than the equivalent structure-sharing trees, under a certain size at least.