GoLang - Sequential vs Concurrent
<p>I have two versions of factorial.  Concurrent vs Sequencial. </p>

<p>Both the program will calculate factorial of 10 "1000000" times.</p>

<h2>Factorial Concurrent Processing</h2>

<pre><code>package main

import (
    "fmt"
    //"math/rand"
    "sync"
    "time"
    //"runtime"
)

func main() {
    start := time.Now()
    printFact(fact(gen(1000000)))
    fmt.Println("Current Time:", time.Now(), "Start Time:", start, "Elapsed Time:", time.Since(start))
    panic("Error Stack!")
}

func gen(n int) &lt;-chan int {
    c := make(chan int)
    go func() {
        for i := 0; i &lt; n; i++ {
            //c &lt;- rand.Intn(10) + 1
            c &lt;- 10
        }
        close(c)
    }()
    return c
}

func fact(in &lt;-chan int) &lt;-chan int {
    out := make(chan int)
    var wg sync.WaitGroup
    for n := range in {
        wg.Add(1)
        go func(n int) {
            //temp := 1
            //for i := n; i &gt; 0; i-- {
            //  temp *= i
            //}
            temp := calcFact(n)
            out &lt;- temp
            wg.Done()
        }(n)
    }
    go func() {
        wg.Wait()
        close(out)
    }()
    return out
}

func printFact(in &lt;-chan int) {
    //for n := range in {
    //  fmt.Println("The random Factorial is:", n)
    //}
    var i int
    for range in {
        i ++
    }
    fmt.Println("Count:" , i)
}

func calcFact(c int) int {
    if c == 0 {
        return 1
    } else {
        return calcFact(c-1) * c
    }
}

//###End of Factorial Concurrent 
</code></pre>

<h2>Factorial Sequencial Processing</h2>

<pre><code>package main

import (
    "fmt"
    //"math/rand"
    "time"
    "runtime"
)

func main() {
    start := time.Now()
    //for _, n := range factorial(gen(10000)...) {
    //  fmt.Println("The random Factorial is:", n)
    //}
    var i int
    for range factorial(gen(1000000)...) {
        i++
    }
    fmt.Println("Count:" , i)
    fmt.Println("Current Time:", time.Now(), "Start Time:", start, "Elapsed Time:", time.Since(start))
}

func gen(n int) []int {
    var out []int
    for i := 0; i &lt; n; i++ {
        //out = append(out, rand.Intn(10)+1)
        out = append(out, 10)
    }
    println(len(out))
    return out
}

func factorial(val ...int) []int {
    var out []int
    for _, n := range val {
        fa := calcFact(n)
        out = append(out, fa)
    }
    return out
}

func calcFact(c int) int {
    if c == 0 {
        return 1
    } else {
        return calcFact(c-1) * c
    }
}

//###End of Factorial sequential processing
</code></pre>

<p>My assumption was concurrent processing will be faster than sequential but sequential is executing faster than concurrent in my windows machine.</p>

<p>I am using 8 core/ i7 / 32 GB RAM. </p>

<p>I am not sure if there is something wrong in the programs or my basic understanding is correct.</p>

<p>p.s. - I am new to GoLang.  </p>