Go Interface vs Direct Method Call Benchmark

A benchmark to compare the performance of interface vs direct method calls in Go.

Description:

This benchmark compares the performance of interface vs direct method calls in Go. Each benchmark creates a struct, containing a single method. It will then call the method on the struct, either directly or via an interface.

Implementations

Green marks the fastest implementation, red marks the slowest

Comparison

This section compares the performance of Interface vs Direct Method Call implementations with the functions: run

Different Run Count

Different CPU Core Count

Direct Method Call

This benchmark calls the method on the struct directly.

Function ns/op ops/sec B/op allocs/op MB/s
run 0.35 2981073063 0.0 0 0.0

Comparison

  • Direct Method Call run is 79.77% faster than Interface Method Call run.
func BenchmarkDirectMethodCall_run(b *testing.B) {
	s := DirectStruct{}
	for i := 0; i < b.N; i++ {
		s.Method()
	}
}

Direct Method Call run

ns/op ops/sec B/op allocs/op MB/s
0.35 2981073063 0.0 0 0.0
By Run Count
By CPU Core Count

Interface Method Call

This benchmark calls the method on the struct via an interface.

Function ns/op ops/sec B/op allocs/op MB/s
run 1.72 586007454 0.0 0 0.0

Comparison

  • Interface Method Call run is 394.33% slower than Direct Method Call run.
func BenchmarkInterfaceMethodCall_run(b *testing.B) {
	var s Interface = InterfaceStruct{}
	for i := 0; i < b.N; i++ {
		s.Method()
	}
}

Interface Method Call run

ns/op ops/sec B/op allocs/op MB/s
1.72 586007454 0.0 0 0.0
By Run Count
By CPU Core Count

Full Benchmark Code

type DirectStruct struct{}

func (m DirectStruct) Method() {}

func BenchmarkDirectMethodCall_run(b *testing.B) {
	s := DirectStruct{}
	for i := 0; i < b.N; i++ {
		s.Method()
	}
}

type Interface interface {
	Method()
}

type InterfaceStruct struct{}

func (m InterfaceStruct) Method() {}

func BenchmarkInterfaceMethodCall_run(b *testing.B) {
	var s Interface = InterfaceStruct{}
	for i := 0; i < b.N; i++ {
		s.Method()
	}
}

Full Benchmark Output

Implementation Function Runs CPU Core Count ns/op ops/sec B/op allocs/op MB/s
Direct Method Call run 1000 2 0.48 2083333333.3333335 0 0 0
Direct Method Call run 2000 2 0.38 2631578947.368421 0 0 0
Direct Method Call run 3000 2 0.3333 3000300030.0030003 0 0 0
Direct Method Call run 4000 2 0.3225 3100775193.7984495 0 0 0
Direct Method Call run 5000 2 0.316 3164556962.025316 0 0 0
Direct Method Call run 6000 2 0.2983 3352329869.259135 0 0 0
Direct Method Call run 7000 2 0.4971 2011667672.500503 0 0 0
Direct Method Call run 8000 2 0.3013 3318951211.417192 0 0 0
Direct Method Call run 9000 2 0.29 3448275862.068966 0 0 0
Direct Method Call run 10000 2 0.293 3412969283.2764506 0 0 0
Direct Method Call run 1000 8 0.45 2222222222.2222223 0 0 0
Direct Method Call run 2000 8 0.63 1587301587.3015873 0 0 0
Direct Method Call run 3000 8 0.3467 2884338044.4188056 0 0 0
Direct Method Call run 4000 8 0.3325 3007518796.992481 0 0 0
Direct Method Call run 5000 8 0.354 2824858757.062147 0 0 0
Direct Method Call run 6000 8 0.305 3278688524.590164 0 0 0
Direct Method Call run 7000 8 0.3043 3286230693.394676 0 0 0
Direct Method Call run 8000 8 0.3013 3318951211.417192 0 0 0
Direct Method Call run 9000 8 0.2944 3396739130.4347825 0 0 0
Direct Method Call run 10000 8 0.294 3401360544.217687 0 0 0
Direct Method Call run 1000 16 0.53 1886792452.8301885 0 0 0
Direct Method Call run 2000 16 0.385 2597402597.4025974 0 0 0
Direct Method Call run 3000 16 0.3467 2884338044.4188056 0 0 0
Direct Method Call run 4000 16 0.325 3076923076.9230766 0 0 0
Direct Method Call run 5000 16 0.31 3225806451.612903 0 0 0
Direct Method Call run 6000 16 0.31 3225806451.612903 0 0 0
Direct Method Call run 7000 16 0.2986 3348961821.8352313 0 0 0
Direct Method Call run 8000 16 0.2938 3403675970.0476513 0 0 0
Direct Method Call run 9000 16 0.2956 3382949932.3410015 0 0 0
Direct Method Call run 10000 16 0.295 3389830508.4745765 0 0 0
Direct Method Call run 1000 1 0.45 2222222222.2222223 0 0 0
Direct Method Call run 2000 1 0.36 2777777777.7777777 0 0 0
Direct Method Call run 3000 1 0.5533 1807337791.4332187 0 0 0
Direct Method Call run 4000 1 0.315 3174603174.6031747 0 0 0
Direct Method Call run 5000 1 0.304 3289473684.2105265 0 0 0
Direct Method Call run 6000 1 0.2983 3352329869.259135 0 0 0
Direct Method Call run 7000 1 0.33 3030303030.30303 0 0 0
Direct Method Call run 8000 1 0.2925 3418803418.803419 0 0 0
Direct Method Call run 9000 1 0.29 3448275862.068966 0 0 0
Direct Method Call run 10000 1 0.287 3484320557.4912896 0 0 0
Direct Method Call run 1000 4 0.51 1960784313.72549 0 0 0
Direct Method Call run 2000 4 0.385 2597402597.4025974 0 0 0
Direct Method Call run 3000 4 0.3467 2884338044.4188056 0 0 0
Direct Method Call run 4000 4 0.3225 3100775193.7984495 0 0 0
Direct Method Call run 5000 4 0.322 3105590062.111801 0 0 0
Direct Method Call run 6000 4 0.3033 3297065611.605671 0 0 0
Direct Method Call run 7000 4 0.4671 2140869192.8923142 0 0 0
Direct Method Call run 8000 4 0.295 3389830508.4745765 0 0 0
Direct Method Call run 9000 4 0.2889 3461405330.564209 0 0 0
Direct Method Call run 10000 4 0.294 3401360544.217687 0 0 0
Direct Method Call run 1000 32 0.5 2000000000 0 0 0
Direct Method Call run 2000 32 0.385 2597402597.4025974 0 0 0
Direct Method Call run 3000 32 0.35 2857142857.1428576 0 0 0
Direct Method Call run 4000 32 0.325 3076923076.9230766 0 0 0
Direct Method Call run 5000 32 0.32 3125000000 0 0 0
Direct Method Call run 6000 32 0.3117 3208213025.344883 0 0 0
Direct Method Call run 7000 32 0.3014 3317850033.1785 0 0 0
Direct Method Call run 8000 32 0.3 3333333333.3333335 0 0 0
Direct Method Call run 9000 32 0.2911 3435245620.0618343 0 0 0
Direct Method Call run 10000 32 0.293 3412969283.2764506 0 0 0
Interface Method Call run 1000 1 1.82 549450549.4505495 0 0 0
Interface Method Call run 2000 1 1.71 584795321.6374269 0 0 0
Interface Method Call run 3000 1 1.677 596302921.8843172 0 0 0
Interface Method Call run 4000 1 1.67 598802395.2095809 0 0 0
Interface Method Call run 5000 1 1.664 600961538.4615384 0 0 0
Interface Method Call run 6000 1 1.652 605326876.5133172 0 0 0
Interface Method Call run 7000 1 1.646 607533414.3377886 0 0 0
Interface Method Call run 8000 1 1.642 609013398.2947625 0 0 0
Interface Method Call run 9000 1 1.639 610128126.9066504 0 0 0
Interface Method Call run 10000 1 1.639 610128126.9066504 0 0 0
Interface Method Call run 1000 4 1.85 540540540.5405405 0 0 0
Interface Method Call run 2000 4 1.8 555555555.5555556 0 0 0
Interface Method Call run 3000 4 1.69 591715976.3313609 0 0 0
Interface Method Call run 4000 4 1.675 597014925.3731343 0 0 0
Interface Method Call run 5000 4 1.672 598086124.4019139 0 0 0
Interface Method Call run 6000 4 1.66 602409638.5542169 0 0 0
Interface Method Call run 7000 4 1.649 606428138.2656155 0 0 0
Interface Method Call run 8000 4 1.65 606060606.0606061 0 0 0
Interface Method Call run 9000 4 1.641 609384521.6331505 0 0 0
Interface Method Call run 10000 4 1.859 537923614.8466917 0 0 0
Interface Method Call run 1000 32 1.85 540540540.5405405 0 0 0
Interface Method Call run 2000 32 1.73 578034682.0809249 0 0 0
Interface Method Call run 3000 32 1.69 591715976.3313609 0 0 0
Interface Method Call run 4000 32 1.675 597014925.3731343 0 0 0
Interface Method Call run 5000 32 1.672 598086124.4019139 0 0 0
Interface Method Call run 6000 32 1.658 603136308.8057902 0 0 0
Interface Method Call run 7000 32 1.65 606060606.0606061 0 0 0
Interface Method Call run 8000 32 1.647 607164541.5907711 0 0 0
Interface Method Call run 9000 32 1.644 608272506.082725 0 0 0
Interface Method Call run 10000 32 1.647 607164541.5907711 0 0 0
Interface Method Call run 1000 2 1.82 549450549.4505495 0 0 0
Interface Method Call run 2000 2 1.72 581395348.8372093 0 0 0
Interface Method Call run 3000 2 1.69 591715976.3313609 0 0 0
Interface Method Call run 4000 2 1.68 595238095.2380953 0 0 0
Interface Method Call run 5000 2 1.664 600961538.4615384 0 0 0
Interface Method Call run 6000 2 1.652 605326876.5133172 0 0 0
Interface Method Call run 7000 2 2.021 494804552.2018803 0 0 0
Interface Method Call run 8000 2 1.651 605693519.0793458 0 0 0
Interface Method Call run 9000 2 3.289 304043782.30465186 0 0 0
Interface Method Call run 10000 2 1.643 608642726.7194157 0 0 0
Interface Method Call run 1000 8 1.83 546448087.4316939 0 0 0
Interface Method Call run 2000 8 1.745 573065902.5787965 0 0 0
Interface Method Call run 3000 8 1.69 591715976.3313609 0 0 0
Interface Method Call run 4000 8 1.673 597728631.2014345 0 0 0
Interface Method Call run 5000 8 1.67 598802395.2095809 0 0 0
Interface Method Call run 6000 8 1.66 602409638.5542169 0 0 0
Interface Method Call run 7000 8 1.651 605693519.0793458 0 0 0
Interface Method Call run 8000 8 1.651 605693519.0793458 0 0 0
Interface Method Call run 9000 8 1.644 608272506.082725 0 0 0
Interface Method Call run 10000 8 1.642 609013398.2947625 0 0 0
Interface Method Call run 1000 16 1.86 537634408.6021506 0 0 0
Interface Method Call run 2000 16 1.74 574712643.6781609 0 0 0
Interface Method Call run 3000 16 1.697 589275191.5144372 0 0 0
Interface Method Call run 4000 16 1.675 597014925.3731343 0 0 0
Interface Method Call run 5000 16 1.666 600240096.0384154 0 0 0
Interface Method Call run 6000 16 1.658 603136308.8057902 0 0 0
Interface Method Call run 7000 16 1.653 604960677.5559589 0 0 0
Interface Method Call run 8000 16 1.647 607164541.5907711 0 0 0
Interface Method Call run 9000 16 1.643 608642726.7194157 0 0 0
Interface Method Call run 10000 16 1.648 606796116.5048544 0 0 0