Created
November 5, 2014 14:05
-
-
Save neguse/010df7cb00bdc2be104b to your computer and use it in GitHub Desktop.
VectorとMatrixを実装する時に値渡しと参照渡しとでどのぐらい差が出るか調べてみた
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 box2dlite | |
type Vec2 struct { | |
X, Y float32 | |
} | |
func V2Add_Copy(v1 Vec2, v2 Vec2) Vec2 { | |
return Vec2{v1.X + v2.X, v1.Y + v2.Y} | |
} | |
func (v *Vec2) V2Add_Ref1 (v1 *Vec2, v2 *Vec2) { | |
v.X = v1.X + v2.X | |
v.Y = v1.Y + v2.Y | |
} | |
func (v *Vec2) V2Add_Ref2 (v1 *Vec2, v2 *Vec2) { | |
*v = Vec2{v1.X + v2.X, v1.Y + v2.Y} | |
} | |
type Mat22 struct { | |
Col1, Col2 Vec2 | |
} | |
type Mat22_2 struct { | |
X1, X2, Y1, Y2 float32 | |
} | |
func M22Add_Copy1(m1 Mat22, m2 Mat22) Mat22 { | |
return Mat22{V2Add_Copy(m1.Col1, m2.Col1), V2Add_Copy(m1.Col2, m2.Col2)} | |
} | |
func M22Add_Copy2(m1 Mat22, m2 Mat22) Mat22 { | |
return Mat22{ | |
Vec2{m1.Col1.X + m2.Col1.X, m1.Col1.Y + m2.Col1.Y}, | |
Vec2{m1.Col2.X + m2.Col2.X, m1.Col2.Y + m2.Col2.Y}, | |
} | |
} | |
func (m *Mat22)M22Add_Ref1(m1 *Mat22, m2 *Mat22) { | |
m.Col1.V2Add_Ref1(&m1.Col1, &m2.Col1) | |
m.Col2.V2Add_Ref1(&m1.Col2, &m2.Col2) | |
} | |
func (m *Mat22)M22Add_Ref2(m1 *Mat22, m2 *Mat22) { | |
m.Col1.X = m1.Col1.X + m2.Col1.X | |
m.Col1.Y = m1.Col1.Y + m2.Col1.Y | |
m.Col2.X = m1.Col2.X + m2.Col2.X | |
m.Col2.Y = m1.Col2.Y + m2.Col2.Y | |
} | |
func (m *Mat22)M22Add_Ref3(m1 *Mat22, m2 *Mat22) { | |
m.Col1 = V2Add_Copy(m1.Col1, m2.Col1) | |
m.Col2 = V2Add_Copy(m1.Col2, m2.Col2) | |
} | |
func (m *Mat22_2)M22_2Add_Ref1(m1 *Mat22_2, m2 *Mat22_2) { | |
*m = Mat22_2{ | |
m1.X1 + m2.X1, | |
m1.Y1 + m2.Y1, | |
m1.X2 + m2.X2, | |
m1.Y2 + m2.Y2, | |
} | |
} | |
func (m *Mat22_2)M22_2Add_Ref2(m1 *Mat22_2, m2 *Mat22_2) { | |
m.X1 = m1.X1 + m2.X1 | |
m.Y1 = m1.Y1 + m2.Y1 | |
m.X2 = m1.X2 + m2.X2 | |
m.Y2 = m1.Y2 + m2.Y2 | |
} | |
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 box2dlite | |
import ( | |
"testing" | |
) | |
func PrintVec2 (v *Vec2) { | |
} | |
func BenchmarkV2Add_Copy(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var a, b, c Vec2 | |
c = V2Add_Copy(a, b) | |
PrintVec2(&c) | |
} | |
} | |
func BenchmarkV2Add_Ref1(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var a, b, c Vec2 | |
c.V2Add_Ref1(&a, &b) | |
PrintVec2(&c) | |
} | |
} | |
func BenchmarkV2Add_Ref2(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var a, b, c Vec2 | |
c.V2Add_Ref2(&a, &b) | |
PrintVec2(&c) | |
} | |
} | |
func BenchmarkM22Add_Copy1(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var a, b Mat22 | |
var _ = M22Add_Copy1(a, b) | |
} | |
} | |
func BenchmarkM22Add_Copy2(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var a, b Mat22 | |
var _ = M22Add_Copy2(a, b) | |
} | |
} | |
func BenchmarkM22Add_Ref1(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var a, b, c Mat22 | |
c.M22Add_Ref1(&a, &b) | |
} | |
} | |
func BenchmarkM22Add_Ref2(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var a, b, c Mat22 | |
c.M22Add_Ref2(&a, &b) | |
} | |
} | |
func BenchmarkM22Add_Ref3(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var a, b, c Mat22 | |
c.M22Add_Ref3(&a, &b) | |
} | |
} | |
func BenchmarkM22_2Add_Ref1(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var a, b, c Mat22_2 | |
c.M22_2Add_Ref1(&a, &b) | |
} | |
} | |
func BenchmarkM22_2Add_Ref2(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
var a, b, c Mat22_2 | |
c.M22_2Add_Ref2(&a, &b) | |
} | |
} |
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
$ go test --bench . | |
testing: warning: no tests to run | |
PASS | |
BenchmarkV2Add_Copy 500000000 3.94 ns/op | |
BenchmarkV2Add_Ref1 500000000 4.74 ns/op | |
BenchmarkV2Add_Ref2 500000000 5.48 ns/op | |
BenchmarkM22Add_Copy1 50000000 34.9 ns/op | |
BenchmarkM22Add_Copy2 50000000 31.0 ns/op | |
BenchmarkM22Add_Ref1 100000000 13.3 ns/op | |
BenchmarkM22Add_Ref2 100000000 10.7 ns/op | |
BenchmarkM22Add_Ref3 100000000 12.6 ns/op | |
BenchmarkM22_2Add_Ref1 100000000 17.2 ns/op | |
BenchmarkM22_2Add_Ref2 100000000 17.2 ns/op |
BenchmarkM22Add_Ref2とBenchmarkM22_2Add_Ref2の違いが気になる
そんなに速度差があるようには見えない
go version go1.3.1 darwin/amd64
go tool 6gでアセンブルコードみてみたところ、Add関数の呼び出し側のコードが違う
structを0初期化するところ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参照渡しでいい気がする