Skip to content

Instantly share code, notes, and snippets.

@qRoC
Created November 19, 2015 16:26
Show Gist options
  • Save qRoC/13d93a5223ce8401a4e9 to your computer and use it in GitHub Desktop.
Save qRoC/13d93a5223ce8401a4e9 to your computer and use it in GitHub Desktop.
& vs new
BenchmarkNewV1Arr-8 2000000000 1.86 ns/op
BenchmarkNewV1Struct-8 1000000000 2.04 ns/op
BenchmarkNewV2Arr-8 1000000000 3.18 ns/op
BenchmarkNewV2Struct-8 500000000 3.24 ns/op
package math
import (
//"fmt"
//"math"
)
type Vec2 [4]float32
type Vec22 struct {
x, y, z, w float32
}
func NewV1Vec2Arr(x, y float32) *Vec2 {
vec2 := new(Vec2)
vec2[0], vec2[1] = x, y
return vec2
}
func NewV1Vec2Struct(x, y float32) *Vec22 {
vec2 := new(Vec22)
vec2.x, vec2.y = x, y
return vec2
}
func NewV2Vec2Arr(x, y float32) *Vec2 {
return &Vec2{x, y}
}
func NewV2Vec2Struct(x, y float32) *Vec22 {
return &Vec22{x:x, y:y}
}
package math
import "testing"
func BenchmarkNewV1Arr(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewV1Vec2Arr(1, 4)
}
}
func BenchmarkNewV1Struct(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewV1Vec2Struct(1, 4)
}
}
func BenchmarkNewV2Arr(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewV2Vec2Arr(1, 4)
}
}
func BenchmarkNewV2Struct(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = NewV2Vec2Struct(1, 4)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment