Last active
May 16, 2017 14:48
-
-
Save shibukawa/b9acb32765a87a63c24816b8c89e6907 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 main | |
import ( | |
"testing" | |
"math/rand" | |
) | |
type Mat32_32 [6]float32 | |
func NewMat32_32() *Mat32_32 { | |
result := &Mat32_32{} | |
for i := 0; i < 6; i++ { | |
result[i] = rand.Float32() | |
} | |
return result | |
} | |
func (t Mat32_32) CopyMultiply(s Mat32_32) Mat32_32 { | |
t0 := t[0]*s[0] + t[1]*s[2] | |
t2 := t[2]*s[0] + t[3]*s[2] | |
t4 := t[4]*s[0] + t[5]*s[2] + s[4] | |
t[1] = t[0]*s[1] + t[1]*s[3] | |
t[3] = t[2]*s[1] + t[3]*s[3] | |
t[5] = t[4]*s[1] + t[5]*s[3] + s[5] | |
t[0] = t0 | |
t[2] = t2 | |
t[4] = t4 | |
return t | |
} | |
func (t *Mat32_32) InlineMultiply(s Mat32_32) *Mat32_32 { | |
t0 := t[0]*s[0] + t[1]*s[2] | |
t2 := t[2]*s[0] + t[3]*s[2] | |
t4 := t[4]*s[0] + t[5]*s[2] + s[4] | |
t[1] = t[0]*s[1] + t[1]*s[3] | |
t[3] = t[2]*s[1] + t[3]*s[3] | |
t[5] = t[4]*s[1] + t[5]*s[3] + s[5] | |
t[0] = t0 | |
t[2] = t2 | |
t[4] = t4 | |
return t | |
} | |
type Mat32_64 [6]float64 | |
func NewMat32_64() *Mat32_64 { | |
result := &Mat32_64{} | |
for i := 0; i < 6; i++ { | |
result[i] = rand.Float64() | |
} | |
return result | |
} | |
func (t Mat32_64) CopyMultiply(s Mat32_64) Mat32_64 { | |
t0 := t[0]*s[0] + t[1]*s[2] | |
t2 := t[2]*s[0] + t[3]*s[2] | |
t4 := t[4]*s[0] + t[5]*s[2] + s[4] | |
t[1] = t[0]*s[1] + t[1]*s[3] | |
t[3] = t[2]*s[1] + t[3]*s[3] | |
t[5] = t[4]*s[1] + t[5]*s[3] + s[5] | |
t[0] = t0 | |
t[2] = t2 | |
t[4] = t4 | |
return t | |
} | |
func (t *Mat32_64) InlineMultiply(s Mat32_64) *Mat32_64 { | |
t0 := t[0]*s[0] + t[1]*s[2] | |
t2 := t[2]*s[0] + t[3]*s[2] | |
t4 := t[4]*s[0] + t[5]*s[2] + s[4] | |
t[1] = t[0]*s[1] + t[1]*s[3] | |
t[3] = t[2]*s[1] + t[3]*s[3] | |
t[5] = t[4]*s[1] + t[5]*s[3] + s[5] | |
t[0] = t0 | |
t[2] = t2 | |
t[4] = t4 | |
return t | |
} | |
func BenchmarkMat32_32_InlineMultiply(b *testing.B) { | |
matrices := make([]Mat32_32, 2000) | |
for i := 0; i < 2000; i++ { | |
matrices[i] = *NewMat32_32() | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
matrices[i % 1000].InlineMultiply(matrices[i % 1000 + 1000]) | |
} | |
} | |
func BenchmarkMat32_32_CopyMultiply(b *testing.B) { | |
matrices := make([]Mat32_32, 2000) | |
for i := 0; i < 2000; i++ { | |
matrices[i] = *NewMat32_32() | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
matrices[i % 1000].CopyMultiply(matrices[i % 1000 + 1000]) | |
} | |
} | |
func BenchmarkMat32_64_InlineMultiply(b *testing.B) { | |
matrices := make([]Mat32_64, 2000) | |
for i := 0; i < 2000; i++ { | |
matrices[i] = *NewMat32_64() | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
matrices[i % 1000].InlineMultiply(matrices[i % 1000 + 1000]) | |
} | |
} | |
func BenchmarkMat32_64_CopyMultiply(b *testing.B) { | |
matrices := make([]Mat32_64, 2000) | |
for i := 0; i < 2000; i++ { | |
matrices[i] = *NewMat32_64() | |
} | |
b.ResetTimer() | |
for i := 0; i < b.N; i++ { | |
matrices[i % 1000].CopyMultiply(matrices[i % 1000 + 1000]) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment