-
-
Save sirupsen/d413b130d0f45d0d35d0bc85b9071abb 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 ( | |
"math/rand" | |
"sync" | |
) | |
type Cell88 struct { | |
padding [80]byte | |
value float64 | |
} | |
type Cell64 struct { | |
padding [56]byte | |
value float64 | |
} | |
type Cell32 struct { | |
padding [24]byte | |
value float64 | |
} | |
type Cell16 struct { | |
padding [8]byte | |
value float64 | |
} | |
type Cell8 struct { | |
value float64 | |
} | |
type Cell = Cell8 | |
func main() { | |
// pointerMapIntegerIndex(10_000_000) | |
// pointerMapIntegerIndexPreallocated(10_000_000) | |
// arrayCellValues(10_000_000) | |
// parallelArrayCellValues(333_333_333) | |
parallelArrayCellValuesWithOwnership(10_000_000) | |
} | |
func pointerMapIntegerIndex(nCells int) { | |
one := make(map[int]*Cell, nCells) | |
two := make(map[int]*Cell, nCells) | |
res := make(map[int]*Cell, nCells) | |
rand := rand.New(rand.NewSource(0xCA0541)) | |
for i := 0; i < nCells; i++ { | |
one[i] = &Cell{value: rand.Float64()} | |
two[i] = &Cell{value: rand.Float64()} | |
} | |
for i := 0; i < nCells; i++ { | |
res[i] = &Cell{value: one[i].value * two[i].value} | |
} | |
} | |
func pointerMapIntegerIndexPreallocated(nCells int) { | |
oneCells := make([]Cell, nCells) | |
twoCells := make([]Cell, nCells) | |
resCells := make([]Cell, nCells) | |
one := make(map[int]*Cell, nCells) | |
two := make(map[int]*Cell, nCells) | |
res := make(map[int]*Cell, nCells) | |
rand := rand.New(rand.NewSource(0xCA0541)) | |
for i := 0; i < nCells; i++ { | |
oneCells[i].value = rand.Float64() | |
twoCells[i].value = rand.Float64() | |
one[i] = &oneCells[i] | |
two[i] = &twoCells[i] | |
} | |
for i := 0; i < nCells; i++ { | |
resCells[i].value = one[i].value * two[i].value | |
res[i] = &resCells[i] | |
} | |
} | |
func arrayCellValues(nCells int) { | |
one := make([]Cell, nCells) | |
two := make([]Cell, nCells) | |
res := make([]Cell, nCells) | |
rand := rand.New(rand.NewSource(0xCA0541)) | |
for i := 0; i < nCells; i++ { | |
one[i].value = rand.Float64() | |
two[i].value = rand.Float64() | |
} | |
for i := 0; i < nCells; i++ { | |
res[i].value = one[i].value * two[i].value | |
} | |
} | |
func parallelArrayCellValues(nCells int) { | |
one := make([]Cell, nCells) | |
two := make([]Cell, nCells) | |
res := make([]Cell, nCells) | |
nThreads := 1 | |
var wg sync.WaitGroup | |
for i := 0; i < nCells; i += nCells / nThreads { | |
wg.Add(1) | |
go func(i int) { | |
defer wg.Done() | |
rand := rand.New(rand.NewSource(0xCA0541)) | |
end := i + nCells / nThreads | |
if (end > nCells) { | |
end = nCells | |
} | |
for j := i; j < end; j++ { | |
one[j].value = rand.Float64() | |
two[j].value = rand.Float64() | |
} | |
for j := i; j < i + nCells / nThreads; j++ { | |
res[j].value = one[j].value * two[j].value | |
} | |
}(i) | |
} | |
wg.Wait() | |
} | |
func parallelArrayCellValuesWithOwnership(nCells int) { | |
nThreads := 8 | |
var wg sync.WaitGroup | |
resCh := make(chan []Cell) | |
for i := 0; i < nCells; i += nCells / nThreads { | |
wg.Add(1) | |
go func() { | |
wg.Done() | |
one := make([]Cell, nCells / nThreads) | |
two := make([]Cell, nCells / nThreads) | |
res := make([]Cell, nCells / nThreads) | |
rand := rand.New(rand.NewSource(0xCA0541)) | |
for j := 0; j < nCells / nThreads; j++ { | |
one[j].value = rand.Float64() | |
two[j].value = rand.Float64() | |
} | |
for j := 0; j < nCells / nThreads; j++ { | |
res[j].value = one[j].value * two[j].value | |
} | |
resCh <- res | |
}() | |
} | |
wg.Wait() | |
<-resCh | |
<-resCh | |
<-resCh | |
<-resCh | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment