Skip to content

Instantly share code, notes, and snippets.

@eliasdaler
Created December 13, 2021 21:16
Show Gist options
  • Save eliasdaler/14ab710e06bf3fc0ba0a6905db76b964 to your computer and use it in GitHub Desktop.
Save eliasdaler/14ab710e06bf3fc0ba0a6905db76b964 to your computer and use it in GitHub Desktop.
Comparison (struct vs kvatbog's vector which uses slices)
package main
import (
"testing"
"github.com/kvartborg/vector"
)
// kvartborg/vector version
type vec = vector.Vector
func BenchmarkVector(bench *testing.B) {
a := vec{10, 20}
b := vec{20, 30}
for i := 0; i < bench.N; i++ {
c := a.Add(b)
_ = c
}
}
// slice version!
type Vec2f struct {
X, Y float64
}
func (v Vec2f) Add(o Vec2f) Vec2f {
return Vec2f{v.X + o.X, v.Y + o.Y}
}
func BenchmarkVec2f(bench *testing.B) {
a := Vec2f{10, 20}
b := Vec2f{20, 30}
for i := 0; i < bench.N; i++ {
c := a.Add(b)
_ = c
}
}
goos: linux
goarch: amd64
pkg: test.com/m
cpu: Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
BenchmarkVector-12 78154443 14.82 ns/op 16 B/op 1 allocs/op
BenchmarkVec2f-12 1000000000 0.2362 ns/op 0 B/op 0 allocs/op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment