Reputation: 9622
I am new to Golang.
Should I always avoid append
ing slices?
I need to load a linebreak-separated data file in memory. With performance in mind, should I count lines, then load all the data in a predefined length array, or can I just append lines to a slice?
Upvotes: 2
Views: 1796
Reputation: 42413
You should stop thinking about performance and start measuring what the actual bottleneck of you application is.
Any advice to a question like "Should do/avoid X because of performance?" is useless in 50% of the cases and counterproductive in 25%.
There are a few really general advices like "do not needlessly generate garbage" but your question cannot be answered as this depends a lot on the size of your file:
[]string
slice 4 times (or 0 times you you make([]string,0,100)
it initially). A string
is just 2 words.You see: A general advice for performance is a bad advice so I won't give one.
Upvotes: 11