Golang multithread
IPFS
沒實際驗證一下沒想到差這麼多
package main
import (
"fmt"
"sync"
"time"
)
func elapsedMillis(t0, t1 time.Time) float64 {
n0, n1 := float64(t0.UnixNano()), float64(t1.UnixNano())
return (n1 - n0) / 1e6
}
func mymap() {
var m [10000]int
var wg sync.WaitGroup
wg.Add(10000)
t0 := time.Now()
for i := 0; i < 10000; i++ {
go func(j int) {
defer wg.Done()
time.Sleep(1)
m[j] = j
_ = m[j] + j
}(i)
}
wg.Wait()
t1 := time.Now()
fmt.Printf("go: %fms\n", elapsedMillis(t0, t1))
t0 = time.Now()
for j := 0; j < 10000; j++ {
time.Sleep(1)
m[j] = j
_ = m[j] + j
}
t1 = time.Now()
fmt.Printf("go: %fms\n", elapsedMillis(t0, t1))
}
Output:
go: 12.056320ms
go: 151156.967680ms
Original link: Phanix's Blog
喜欢我的作品吗?别忘了给予支持与赞赏,让我知道在创作的路上有你陪伴,一起延续这份热忱!