-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (44 loc) · 1.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt" //for println
"sync" //for mutex like timers
)
//amd ryzen 3700U, governor mode "ondemand"
//time with on worker: 99% cpu 4:26.98 total
//time with 4 workers: 241% cpu 1:54.50 total
//time with 8 workers: 312% cpu 2:08.18 total
func main(){
jobs := make(chan int, 100) //buffered chanel of ints
result := make(chan int, 100)
var wg sync.WaitGroup //the mutex
wg.Add(1)
fibo := 50 //how many numbers we want to calculate
go worker(jobs, result) //parallel workers
go worker(jobs, result) //parallel workers
go worker(jobs, result) //parallel workers
go worker(jobs, result) //parallel workers
for i:=1; i<=fibo; i++ {
jobs <- i
}
close(jobs)
go func() {
for i:=1; i<=fibo; i++ {
fmt.Printf("Number is %v and the result: %+v\n", i, <-result)
}
wg.Done()
}()
wg.Wait()
return
}
func worker( jobs <-chan int, result chan<- int ){
for n := range jobs {
result <-fib(n)
}
fmt.Println("oooohwheee")
}
func fib(num int) int{
if num <= 1 {
return num
}
return fib(num-1) + fib(num-2)
}