Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "sync"
  6. "time"
  7. )
  8.  
  9. func reduce(arr []float64) float64 {
  10. for count := len(arr); count > 1; {
  11. countNext := 1 + (count-1)/2
  12. wg := sync.WaitGroup{}
  13. for i := 0; i < countNext; i++ {
  14. wg.Add(1)
  15. go func(i int) {
  16. if i+countNext < count {
  17. arr[i] = arr[i] + arr[i+countNext]
  18. }
  19. wg.Done()
  20. }(i)
  21. }
  22. wg.Wait()
  23. count = countNext
  24. }
  25. return arr[0]
  26. }
  27.  
  28. func iterate(arr []float64) float64 {
  29. var sum float64 = 0
  30. for i := 0; i < len(arr); i++ {
  31. sum += arr[i]
  32. }
  33. return sum
  34. }
  35.  
  36. func main() {
  37. length := 1000000
  38. {
  39. arr := make([]float64, length)
  40. for i := 0; i < length; i++ {
  41. arr[i] = float64(i)
  42. }
  43. t := time.Now()
  44. fmt.Println("Sum: ", reduce(arr))
  45. fmt.Println(time.Now().Sub(t).Seconds())
  46. }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement