Mercurial > hg > Members > innparusu > go_examples
changeset 0:fbb932a7214b default tip
Add twice.go
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 30 Jan 2018 14:26:41 +0900 |
parents | |
children | |
files | twice.go |
diffstat | 1 files changed, 52 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/twice.go Tue Jan 30 14:26:41 2018 +0900 @@ -0,0 +1,52 @@ +package main + +import ( + "flag" + "fmt" + "runtime" + "time" +) + +func twice(list []int, prefix int, index int, c chan []int) { + for i := 0; i < prefix; i++ { + list[prefix*index+i] = list[prefix*index+i] * 2; + } + c <- list +} + +func main() { + length := flag.Int("l", 1024, "length") + split := flag.Int("s", 8, "task size") + cpu := flag.Int("cpu", 1, "cpu") + flag.Parse() + + fmt.Println("length: ", *length); + prefix := *length / *split + fmt.Println("length/task: ", prefix); + fmt.Println("cpu :", *cpu); + + list := []int{}; + for i :=0; i < *length; i++ { + list = append(list, i); + } + + runtime.GOMAXPROCS(*cpu) + + c := make(chan []int) + start := time.Now() + for i :=0; i < *split; i++ { + go twice(list, prefix, i, c); + } + + for i :=0; i < *split; i++ { + <- c + } + t := time.Now() + elapsed := t.Sub(start) + fmt.Println(elapsed) + for i :=0; i < *length; i++ { + if (i*2 != list[i]) { + fmt.Println("wrong result") + } + } +}