Skip to content

Instantly share code, notes, and snippets.

@ohta-rh
Last active November 12, 2019 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ohta-rh/0982ee468b3ba00a9a0be98b24c4b4a7 to your computer and use it in GitHub Desktop.
Save ohta-rh/0982ee468b3ba00a9a0be98b24c4b4a7 to your computer and use it in GitHub Desktop.
GOでマルチコアを使い切ってみる
package main
import "fmt"
var from int = 1
var to int = 1000 / 4 * 100 // (1000/4コア*100)
var period int = 1000000 * 1000 // 1000倍重くする
func main() {
ch1 := make(chan bool)
ch2 := make(chan bool)
ch3 := make(chan bool)
ch4 := make(chan bool)
go func() {
for hoge := from; hoge < to; hoge++ {
acc := 0
for i := 1; i <= period; i++ {
acc = acc + i
}
result := fmt.Sprintf("%d", acc)
fmt.Println(result)
}
ch1 <- true
}()
go func() {
for hoge := from; hoge < to; hoge++ {
acc := 0
for i := 1; i <= period; i++ {
acc = acc + i
}
result := fmt.Sprintf("%d", acc)
fmt.Println(result)
}
ch2 <- true
}()
go func() {
for hoge := from; hoge < to; hoge++ {
acc := 0
for i := 1; i <= period; i++ {
acc = acc + i
}
result := fmt.Sprintf("%d", acc)
fmt.Println(result)
}
ch3 <- true
}()
go func() {
for hoge := from; hoge < to; hoge++ {
acc := 0
for i := 1; i <= period; i++ {
acc = acc + i
}
result := fmt.Sprintf("%d", acc)
fmt.Println(result)
}
ch4 <- true
}()
<-ch1
<-ch2
<-ch3
<-ch4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment