Skip to content

Instantly share code, notes, and snippets.

@cybergrind
Created September 27, 2013 20:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cybergrind/6734605 to your computer and use it in GitHub Desktop.
Save cybergrind/6734605 to your computer and use it in GitHub Desktop.
package main
import "fmt"
import "time"
import "runtime"
func main() {
numcpu := runtime.NumCPU()
fmt.Println("NumCPU", numcpu)
//runtime.GOMAXPROCS(numcpu)
runtime.GOMAXPROCS(1)
ch3 := make(chan int)
ch1 := make(chan int, 1000000)
ch2 := make(chan float64)
go func() {
for i := 0; i < 1000000; i++ {
ch1 <- i
}
ch3 <- 1
ch1 <- -1
ch2 <- 0.0
}()
go func() {
fmt.Println("TT", <-ch3)
total := 0.0
for {
t1 := time.Now().UnixNano()
for i := 0; i < 100000; i++ {
m := <-ch1
if m == -1 {
ch2 <- total
}
}
t2 := time.Now().UnixNano()
dt := float64(t2 - t1) / 1000000.0
total += dt
fmt.Println(dt)
}
}()
fmt.Println("Total:", <-ch2, <-ch2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment