type MyStruct struct{
content string
}
func (m MyStruct) String() string{
return m.content
}
type Stringer interface {
String() string
}
//0.32ns
func MyFunctionStruct(str MyStruct) string {
return str.String()
}
//0.32ns
func MyFunctionPointer(str *MyStruct) string {
return str.String()
}
//this is interesting! - 0.33ns
func MyFunctionBlankInterface(str interface{}) string{
o:=str.(MyStruct)
return o.String()
}
//43.3ns!
func MyFunctionInterface(str Stringer) string{
return o.String()
}
package main
import "fmt"
type Greeter interface{
Greet(Name string) string
}
type Greet struct{
}
func (g *Greet) Greet(Name string) string {
return fmt.Sprintf("Hello %s",Name)
}
func Structure(g Greet,Name string) string{
return g.Greet(Name)
}
func Interface(g Greeter,Name string) string{
return g.Greet(Name)
}
func main() {
var G Greeter
G = &Greet{}
fmt.Println(G.Greet("Reinhard"))
}
package main
import "testing"
func BenchmarkGreet_GreetInterface(b *testing.B) {
var G Greeter
G = &Greet{}
for n :=0 ; n < b.N;n++{
_ = G.Greet("Reinhard")
}
}
func BenchmarkGreet_GreetStruct(b *testing.B) {
var G Greet
G = Greet{}
for n :=0 ; n < b.N;n++{
_ = G.Greet("Reinhard")
}
}
func BenchmarkStructure(b *testing.B) {
G := Greet{}
for n :=0 ; n < b.N;n++{
_ = Structure(G,"Reinhard")
}
}
func BenchmarkInterface(b *testing.B) {
G := &Greet{}
for n :=0 ; n < b.N;n++{
_ = Interface(G,"Reinhard")
}
}
Thank you
import "testing"
//======================
// Data Definition
//======================
type myStruct struct {
content string
}
func (s myStruct) String() string {
return s.content
}
type Stringer interface {
String() string
}
func printStruct(str myStruct) string {
return str.String()
}
func printPointer(str *myStruct) string {
return str.String()
}
func printInterface(str Stringer) string {
return str.String()
}
func printBlankInterfaceWithStructAssertion(str interface{}) string {
o := str.(myStruct)
return o.String()
}
func printBlankInterfaceWithInterfaceAssertion(str interface{}) string {
o := str.(Stringer)
return o.String()
}
//=========================
// Benchmark
//=========================
func BenchmarkStruct(b *testing.B) {
data := myStruct{"My data"}
for i := 0; i < b.N; i++ {
printStruct(data)
}
}
func BenchmarkPointer(b *testing.B) {
data := myStruct{"My data"}
for i := 0; i < b.N; i++ {
printPointer(&data)
}
}
func BenchmarkInterface(b *testing.B) {
data := myStruct{"My data"}
for i := 0; i < b.N; i++ {
printInterface(data)
}
}
func BenchmarkBlankInterface_WithStructAssertion(b *testing.B) {
data := myStruct{"My data"}
for i := 0; i < b.N; i++ {
printBlankInterfaceWithStructAssertion(data)
}
}
func BenchmarkBlankInterface_WithInterfaceAssertion(b *testing.B) {
data := myStruct{"My data"}
for i := 0; i < b.N; i++ {
printBlankInterfaceWithInterfaceAssertion(data)
}
}
-j
func (g *Greet) Greet(name string) string {
return name
}
func do(str string){
if str == "" {
panic("abc")
}
}
//so the benchmark code would now look like this
//
// do(printBlabla(data))
-j
--
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...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
That's not to say the difference is unavoidable, but it's pretty much to be expected, I'd argue.
On Monday, July 23, 2018 at 8:39:34 PM UTC+7, Axel Wagner wrote:That's not to say the difference is unavoidable, but it's pretty much to be expected, I'd argue.That was unexpected to me.Many people are aware that there are some overhead associated with using interface. However, I don't think they anticipate the cost would be something like 10x, 20x, or more. I personally expected it to be about 1 to 1.5x slower, which would still be acceptable to me. But 10x slower?! I have seen people using interface extensively and I wonder whether they are aware of the cost.
I am saying maybe we should take a look at this. I could be wrong in my benchmark too, but I think my code is pretty idiomatic - meaning that's what most people would likely write without trying to consciously optimize the allocation and stuffs. Assuming my finding is accurate, I don't think the Go Team intended the cost of the interface to be this much.