Your problem in understanding here isn't code, its conceptual.
Your n is so large that the cap version of the program enjoys an advantage
only for a very short number of iterations. Try making the init cap within
at least 1 or 2 orders of magnitude of n.
Lets assume that when a slice is full, the new slice that is created is
2*len(slice)+1 long (and then the old slice is copied into the new one)
Lets look at the sizes for the zero cap and 100 cap versions as they need
to grow:
Zero cap: 0, 1, 3, 7, 15, 31, 63, 127, 255, 511 ....n >= 1000000
100 cap: 100, 201, 403 ....n >= 1000000
As you can see the 100 cap version only enjoys an advantage for the first 8
resize/copy operations- and each successive resize is more expensive (more
bytes to copy), so those initial ones are actually the comparatively cheap
resizes.
On Wednesday, February 26, 2014 7:10:19 AM UTC-6, JohnGB wrote:Jérôme, could you please explain what you mean. The function that is
called in the benchmark will generate a slice that is at least a million
bytes long, so I don't see how it is only considering the first 25 or 50
bytes.
If you could show me what I'm doing wrong here (preferably with code), I'd
be grateful.
On Wednesday, February 26, 2014 1:57:40 PM UTC+1, Jérôme Champion wrote:So you bench the optimization of the allocation of the first 25, 50
bytes, but you don't care about the final allocation of one millions
bytes..
It's not surprising that you don't see performance improvement.
Le mercredi 26 février 2014 13:47:46 UTC+1, JohnGB a écrit :
I wasn't trying to make withCap() as efficient as possible, but rather I
was testing how much better it would be in situations where I don't know
the final size of the slice, and trying to understand how much of a
performance improvement I would get by setting a capacity.
On Wednesday, February 26, 2014 1:39:14 PM UTC+1, Tamás Gulácsi wrote:
withCap only preallocates 100 bytes, but it shoul n.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit
https://groups.google.com/groups/opt_out.