Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> its like the age-old "+" vs "strings.Builder" debate for concatenation

that's not even a debate. Have you done tests on this? If I remember right, once the string runs over the GC default (I think 1 GB), "+" turns from linear to exponential time. So yeah I guess for most stuff it doesn't matter, but when it does matter its a huge time difference



> once the string runs over the GC default (I think 1 GB),

Not wishing to sound snarky, but is there even a valid use-case for concatenating GB+ quantities of strings (or even hundreds of MB) ?

I mean surely you'll (potentially) run into memory exhaustion issues and other fun things ?


Of course there is. 1 GB is not even that big these days.


> Of course there is

When? Why not use ropes and avoid the copy?


personally I don't see a need for it, but I'm sure someone will do something dumb that requires it


While not Go, in C# you can avoid a ton of heap allocations by using StringBuilder instead of concatenation, and the crossover point for one being more efficient than the other is at something like 4 concatenations. But people will always say nonsense about "premature optimization" the moment you care about performance.


In regards to Go, in most cases its a pointless optimization. Unless the string is quite large, its the same result. Go is nothing like C# in this case.


In python since 2.5 (and im sure other languages implemntations) the compiler/implementation recognize this += use case and implement it as a fast one.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: