Hi,

In the below code example, setting a function variable to a simple function 
makes no memory allocation and is very fast (<1ns).  The case of setting 
the same function variable to a method value is much slower (~30ns) and it 
turns out to be because memory is allocated there. (16 B/op)

I found this surprising.  Can anyone explain why memory is allocated and is 
there a way to avoid this?

Thanks.

-- 
Martin.


package main

import "testing"

func BenchmarkStandAlone(b *testing.B) {
    b.ReportAllocs()
    for i := 0; i < b.N; i++ {
        f1 = standalone
    }
}

func BenchmarkMethod(b *testing.B) {
    b.ReportAllocs()
    for i := 0; i < b.N; i++ {
        f1 = myObj.myMethod
    }
}

var (
    f1    func()
    myObj = &myType{}
)

func standalone() {}

type myType struct{}

func (m *myType) myMethod() {}

-- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to