Skip to content

Instantly share code, notes, and snippets.

@win0err win0err/process-vectors.go Secret
Last active Feb 18, 2018

Embed
What would you like to do?
Умножение матрицы на вектор, без оптимизаций
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) })
}
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
You can’t perform that action at this time.