4 ms·
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
by quchen 13y ago
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.)
- deleted 13y ago[deleted]
- alayne 13y agoIt 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.
- gngeal 13y agoString 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.
- alayne 13y agoData structures like ropes are really more for editing than concatenation.
- phaemon 13y agoI'd never heard of "ropes" in that context and read that as the worst simile ever ("Data structures, like ropes, are...")
- lucian1900 13y agoTrees (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.
- gngeal 13y agoI don't see how branch prediction and caches could dominate performance of logically concatenating a huge amount of text instead of doing it physically.
- dkersten 13y agoI 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.
- lucian1900 13y agoArrays and copies are often faster than the equivalent structure-sharing trees, under a certain size at least.
- pjump 13y ago+1