Last active
October 31, 2020 07:27
-
-
Save Wuvist/214691d698d0bdb850b1888016a3cabe to your computer and use it in GitHub Desktop.
vec performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package vec | |
type vec1 struct { | |
x, y, z, w float64 | |
} | |
//go:noline | |
func (v vec1) addnoline(u vec1) vec1 { | |
return vec1{v.x + u.x, v.y + u.y, v.z + u.z, v.w + u.w} | |
} | |
func (v vec1) add(u vec1) vec1 { | |
return vec1{v.x + u.x, v.y + u.y, v.z + u.z, v.w + u.w} | |
} | |
type vec2 struct { | |
x, y, z, w float64 | |
} | |
//go:noline | |
func (v *vec2) addnoline(u *vec2) *vec2 { | |
v.x, v.y, v.z, v.w = v.x+u.x, v.y+u.y, v.y+u.y, v.w+u.w | |
return v | |
} | |
func (v *vec2) add(u *vec2) *vec2 { | |
v.x, v.y, v.z, v.w = v.x+u.x, v.y+u.y, v.y+u.y, v.w+u.w | |
return v | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package vec | |
import "testing" | |
var u1 vec1 | |
var u2 *vec2 | |
func init() { | |
u1 = vec1{1, 1, 1, 1} | |
u2 = &vec2{1, 1, 1, 1} | |
} | |
func BenchmarkValue(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
v1 := vec1{1, 1, 1, 1} | |
v1 = v1.add(u1) | |
} | |
} | |
func BenchmarkPointer(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
v2 := &vec2{1, 1, 1, 1} | |
v2.add(u2) | |
} | |
} | |
func BenchmarkValueNoinline(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
v1 := vec1{1, 1, 1, 1} | |
v1 = v1.addnoline(u1) | |
} | |
} | |
func BenchmarkPointerNoinline(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
v2 := &vec2{1, 1, 1, 1} | |
v2.addnoline(u2) | |
} | |
} | |
func BenchmarkVec(b *testing.B) { | |
b.ReportAllocs() | |
v1 := vec1{1, 2, 3, 4} | |
v2 := vec1{4, 5, 6, 7} | |
b.Run("vec1", func(b *testing.B) { | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
if i%2 == 0 { | |
v1 = v1.add(v2) | |
} else { | |
v2 = v2.add(v1) | |
} | |
} | |
}) | |
v21 := &vec2{1, 2, 3, 4} | |
v22 := &vec2{4, 5, 6, 7} | |
b.Run("vec2", func(b *testing.B) { | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
if i%2 == 0 { | |
v21.add(v22) | |
} else { | |
v22.add(v21) | |
} | |
} | |
}) | |
} | |
func BenchmarkVecNoinline(b *testing.B) { | |
b.ReportAllocs() | |
v1 := vec1{1, 2, 3, 4} | |
v2 := vec1{4, 5, 6, 7} | |
b.Run("vec1", func(b *testing.B) { | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
if i%2 == 0 { | |
v1 = v1.addnoline(v2) | |
} else { | |
v2 = v2.addnoline(v1) | |
} | |
} | |
}) | |
v21 := &vec2{1, 2, 3, 4} | |
v22 := &vec2{4, 5, 6, 7} | |
b.Run("vec2", func(b *testing.B) { | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
if i%2 == 0 { | |
v21.addnoline(v22) | |
} else { | |
v22.addnoline(v21) | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment