Last active
November 12, 2019 11:32
-
-
Save ohta-rh/0982ee468b3ba00a9a0be98b24c4b4a7 to your computer and use it in GitHub Desktop.
GOでマルチコアを使い切ってみる
This file contains hidden or 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" | |
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