为什么并发代码需要更多时间来执行

I have a function named linearize...I'm trying to speed up its execution but surprised to find that it had become slower. Have I missed something or have messed up with fundamentals..

As per my understanding things should improve..

Thanks,

package main

import (
    "fmt"
    "math"
    "sync"
    "time"
)

var rgb []float64

func linearizeWithWg(v float64, idx int, wg *sync.WaitGroup) {
    defer wg.Done()

    if v <= 0.04045 {
        rgb[idx] = v / 12.92
    } else {
        rgb[idx] = math.Pow((v+0.055)/1.055, 2.4)
    }
}

func linearizeWithGoR(v float64) float64 {
    res := make(chan float64)

    go func(input float64) {
        if input <= 0.04045 {
            res <- input / 12.92
        } else {
            res <- math.Pow((input+0.055)/1.055, 2.4)
        }
    }(v)
    return <-res
}

func linearizeNomal(v float64) float64 {
    if v <= 0.04045 {
        return v / 12.92
    }
    return math.Pow((v+0.055)/1.055, 2.4)
}

func main() {
    start := time.Now()
    const C = 1.0 / 255.0

    //Normal Execution
    for i := 0; i < 100000; i++ {
        linearizeNomal(float64(i) * C * 0.5)
        linearizeNomal(float64(i) * C * 1.5)
        linearizeNomal(float64(i) * C * 4.5)
    }
    elaspsed := time.Since(start)
    fmt.Println(elaspsed)

    //With GoRoutines.. Slow
    start = time.Now()
    for i := 0; i < 100000; i++ {
        linearizeWithGoR(float64(i) * C * 0.5)
        linearizeWithGoR(float64(i) * C * 1.5)
        linearizeWithGoR(float64(i) * C * 2.5)
    }
    elaspsed = time.Since(start)
    fmt.Println(elaspsed)

    //With WaitGroup. Slow

    for i := 0; i < 100000; i++ {
        rgb = make([]float64, 3)
        var wg sync.WaitGroup
        wg.Add(3)
        linearizeWithWg(float64(i)*C*0.5, 0, &wg)
        linearizeWithWg(float64(i)*C*1.5, 1, &wg)
        linearizeWithWg(float64(i)*C*4.5, 2, &wg)
        wg.Wait()
    }
    elaspsed = time.Since(start)
    fmt.Println(elaspsed)

}

The overhead of all the concurrency related functions(channel creation, channel sending, goroutine creation) is way bigger than the two instructions that you execute in each of your goroutine.

In addition, your goroutine version is basically serial because you spawn a goroutine and immediately wait for the result of its channel. The waitgroup version is similar.

Try again with a small number of goroutines each executing a chunk of your loop. @Joker_vD also has a good point to make sure that GOMAXPROCS is bigger than one.

Your problem is that you don't actually do anything concurrently.

in the workgroup example, you need to call go linearizeWithWg(...)

In the goroutine example, you start a goroutine, but then wait for it to end in the function. To run it concurrently, you would need a buffered response channel, and having another goroutine getting the responses