That's really interesting. I'd always assumed (without really thinking
more deeply about it) that amortised appending to a slice was
O(n*log(n)). Here's an empirical demonstration, FWIW:
https://play.golang.org/p/iLGiyexo1A
On 12 November 2017 at 17:56, Bakul Shah wrote:
> On Sun, 12 Nov 2017 17:
On Sun, 12 Nov 2017 17:41:18 + Jesper Louis Andersen
wrote:
>
> I haven't tried it out, but I think it will work because each new slot is
> hit twice: it is filled with data, and it is copied away again. So when you
> expand from n to 2*n, you may be able to arrange that you have exactly n
>
I haven't tried it out, but I think it will work because each new slot is
hit twice: it is filled with data, and it is copied away again. So when you
expand from n to 2*n, you may be able to arrange that you have exactly n
credits in the bank which can be used to pay for the copy. This might
requir
On Sun, 12 Nov 2017 15:45:40 + Jesper Louis Andersen
wrote:
>
> To elaborate on Jan's point:
>
> If you extended capacity every time you called append, then you will have
> to reallocate and copy data quite a lot. So a clever system pushes a little
> bit more capacity in at the end to avoid
To elaborate on Jan's point:
If you extended capacity every time you called append, then you will have
to reallocate and copy data quite a lot. So a clever system pushes a little
bit more capacity in at the end to avoid such copies whenever an append
happens. It is a trade-off between space usage
One could lie about it, I suppose, but then you'd need to have an
additional (hidden) value and that certainly does not seem worth it.
Lucio.
On Sunday, 12 November 2017 13:39:59 UTC+2, Jan Mercl wrote:
>
> On Sun, Nov 12, 2017 at 12:00 PM > wrote:
>
> > The question is capacity.
> > Why not : 0
On Sun, Nov 12, 2017 at 12:00 PM wrote:
> The question is capacity.
> Why not : 0,1,2,3,4,5 as length ?
Because O(n) is better than O(n^2).
--
-j
--
You received this message because you are subscribed to the Google Groups
"golang-nuts" group.
To unsubscribe from this group and stop receiv
Hello
I have tried (and modified) from Golang Tour :
package main
import "fmt"
func main() {
var s []int
printSlice(s)
s = append(s, 1)
printSlice(s)
s = append(s, 2)
printSlice(s)
s = append(s, 3)
printSlice(s)
s = append(s, 4)
printSlice(s)
s = append(s, 5)
printSlice(s)
}
func printSli