-
-
Save win0err/de4d4e83c2f4dfe5ca72c4c777b076ba to your computer and use it in GitHub Desktop.
Умножение матрицы на вектор, без оптимизаций
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 main | |
import ( | |
"fmt" | |
"math" | |
"time" | |
) | |
type V4 [4]float32 | |
type M4 [16]float32 | |
const ( | |
numVectors = 8 * 1024 * 1024 | |
) | |
func M4MultiplyV4(m M4, v V4) V4 { | |
return V4{ | |
v[0]*m[0] + v[1]*m[4] + v[2]*m[8] + v[3]*m[12], | |
v[0]*m[1] + v[1]*m[5] + v[2]*m[9] + v[3]*m[13], | |
v[0]*m[2] + v[1]*m[6] + v[2]*m[10] + v[3]*m[14], | |
v[0]*m[3] + v[1]*m[7] + v[2]*m[11] + v[3]*m[15], | |
} | |
} | |
func multiply(data []V4, m M4) { | |
for i, v := range data { | |
data[i] = M4MultiplyV4(m, v) | |
} | |
} | |
func benchmark(f func()) { | |
best := math.MaxFloat64 | |
for i := 0; i < 100; i++ { | |
start := time.Now() | |
f() | |
end := time.Now() | |
elapsed := end.Sub(start).Seconds() | |
if elapsed < best { | |
best = elapsed | |
} | |
} | |
fmt.Printf("%.0fms\n", best*1000) | |
} | |
func main() { | |
data := make([]V4, numVectors) | |
for i := range data { | |
data[i] = V4{1, 2, 3, 4} | |
} | |
m := M4{ | |
1, 2, 3, 4, | |
5, 6, 7, 8, | |
9, 10, 11, 12, | |
13, 14, 15, 16, | |
} | |
benchmark(func() { multiply(data, m) }) | |
} |
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 build -o process-vectors && ./process-vectors |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment