Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "sync"
- "time"
- )
- func reduce(arr []float64) float64 {
- for count := len(arr); count > 1; {
- countNext := 1 + (count-1)/2
- wg := sync.WaitGroup{}
- for i := 0; i < countNext; i++ {
- wg.Add(1)
- go func(i int) {
- if i+countNext < count {
- arr[i] = arr[i] + arr[i+countNext]
- }
- wg.Done()
- }(i)
- }
- wg.Wait()
- count = countNext
- }
- return arr[0]
- }
- func iterate(arr []float64) float64 {
- var sum float64 = 0
- for i := 0; i < len(arr); i++ {
- sum += arr[i]
- }
- return sum
- }
- func main() {
- length := 1000000
- {
- arr := make([]float64, length)
- for i := 0; i < length; i++ {
- arr[i] = float64(i)
- }
- t := time.Now()
- fmt.Println("Sum: ", reduce(arr))
- fmt.Println(time.Now().Sub(t).Seconds())
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement