Skip to content

Instantly share code, notes, and snippets.

@reflechant
Last active August 29, 2017 21:17
Show Gist options
  • Save reflechant/7e075cf1d2785d74724cef1258a98f64 to your computer and use it in GitHub Desktop.
Save reflechant/7e075cf1d2785d74724cef1258a98f64 to your computer and use it in GitHub Desktop.
unfinished benchmark of parallel calculation in Go (trying to rewrite https://habrahabr.ru/post/336684/)
package main
import (
"math"
"time"
"fmt"
)
type Point struct {
x float64
y float64
z float64
}
func NewMatrix(xlen, ylen int) [][]float64 {
m := make([][]float64, xlen)
for i := 0; i < xlen; i++ {
m[i] = make([]float64, ylen)
}
return m
}
func R(p, q Point) float64 {
rx := p.x - q.x
ry := p.y - q.y
rz := p.z - q.z
return 1 / (1 + math.Sqrt(rx*rx+ry*ry+rz*rz))
}
func Calculate(P []Point, Q []Point, M [][]float64) {
c := make(chan bool)
for i, p := range P {
for j, q := range Q {
go func(c chan bool) {
M[i][j] = R(p, q)
c <- true
}(c)
}
}
for i := 0; i < len(P)*len(Q); i++ {
<-c
}
}
func main() {
m := NewMatrix(5000, 5000)
P := make([]Point, 5000)
Q := make([]Point, 5000)
t := time.Now()
Calculate(P, Q, m)
fmt.Println(time.Since(t))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment