package main
import (
"fmt"
"time"
)
func measureTime(str string) func() {
start := time.Now()
return func() {
fmt.Printf("%s took %v time for execution\n", str, time.Since(start))
}
}
// Sequential uses a sequential algorithm.
func Sequential(words []string) map[rune]int { //array of strings simple solutions
m := make(map[rune]int)
for _, word := range words {
for _, c := range word {
m[c]++
}
}
return m
}
// ConcurrentUnlimited uses a concurrent algorithm based on an
// unlimited fan out pattern.
func Concurrent(text []string) map[rune]int {
ch := make(chan map[rune]int, len(text))
for _, words := range text {
go func(words string) {
lm := make(map[rune]int)
for _, r := range words {
lm[r]++
}
ch <- lm
}(words)
}
all := make(map[rune]int)
for range text {
lm := <-ch
for r, c := range lm {
all[r] += c
}
}
return all
}
func main() {
defer measureTime("freq")()
var words []string = []string{"there is solution to every problems"}
// words :=make([]string,5)
// words = "there is solution to every problems"
//ret :=Sequential(words)528.9us
ret := Concurrent(words)//517.8us
fmt.Println(ret)
// }why the hell both are same in performance
// }
}