Created
December 16, 2023 03:19
-
-
Save broklyngagah/95e522aa6992ed2ce25505f2044f42ad to your computer and use it in GitHub Desktop.
GO Concurrency Performance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
func process(data int) int { | |
time.Sleep(time.Second * 2) | |
return data * 2 | |
} | |
func processData(wg *sync.WaitGroup, resultDest *int, data int) { | |
defer wg.Done() | |
processData := process(data) | |
*resultDest = processData | |
} | |
func main() { | |
start := time.Now() | |
var wg sync.WaitGroup | |
input := []int{1, 2, 3, 4, 5} | |
result := make([]int, len(input)) | |
for i, v := range input { | |
wg.Add(1) | |
go processData(&wg, &result[i], v) | |
} | |
wg.Wait() | |
fmt.Println(time.Since(start)) | |
fmt.Println(result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment