On Thursday, January 26, 2017 at 11:15:39 PM UTC+8, andrey mirtchovski wrote: > > I bet you get wildly differing results with each run. That's an > indicator that your sample size is not statistically relevant (there's > randomness introduced both in the select, as well as in the OS and Go > schedulers). > > Increase the iterations until your results stabilize, then you'll see > what the real behaviour is: > > $ for i in 100000 1000000 10000000 100000000 1000000000; do echo $i; > go run t.go $i; done > 100000 > pass one: 48154/51846=0.93 > pass two: 41040/58960=0.70 > pass three: 54133/45867=1.18 > 1000000 > pass one: 531420/468580=1.13 > pass two: 671204/328796=2.04 > pass three: 481769/518231=0.93 > 10000000 > pass one: 4983060/5016940=0.99 > pass two: 5475692/4524308=1.21 > pass three: 4865659/5134341=0.95 > 100000000 > pass one: 50361915/49638085=1.01 > pass two: 54589075/45410925=1.20 > pass three: 47290086/52709914=0.90 >
The result for 1000000000 is the same as 300000 on my machine. It looks the reason is starvation (see my last comment). > > On Thu, Jan 26, 2017 at 5:06 AM, T L <tapi...@gmail.com <javascript:>> > wrote: > > > > package main > > > > import "fmt" > > > > func main() { > > c0, c1 := make(chan int, 10), make(chan int, 10) > > > > go func() { > > for { > > c0 <- 0 > > } > > }() > > > > go func() { > > for { > > c1 <- 1 > > } > > }() > > > > var n0, n1 int > > > > f0 := func() { > > n0++ > > } > > > > f1 := func() { > > n1++ > > } > > > > // pass one > > > > n0, n1 = 0, 0 > > for range [300000]struct{}{} { > > select { > > case <- c0: f0() > > case <- c1: f1() > > } > > } > > > > fmt.Printf("pass one: %d/%d=%.2f \n", n0, n1, float64(n0) / > float64(n1)) > > > > // pass two > > > > n0, n1 = 0, 0 > > for range [300000]struct{}{} { > > select { > > case <- c0: f0() > > case <- c1: f1() > > case <- c1: f1() > > } > > } > > > > fmt.Printf("pass two: %d/%d=%.2f \n", n0, n1, float64(n0) / > float64(n1)) > > > > // pass three > > > > n0, n1 = 0, 0 > > for range [300000]struct{}{} { > > select { > > case <- c0: f0() > > case <- c0: f0() > > case <- c1: f1() > > } > > } > > > > fmt.Printf("pass three: %d/%d=%.2f \n", n0, n1, float64(n0) / > > float64(n1)) > > } > > > > The output: > > > > pass one: 154970/145030=1.07 > > pass two: 177853/122147=1.46 > > pass three: 127768/172232=0.74 > > > > -- > > You received this message because you are subscribed to the Google > Groups > > "golang-nuts" group. > > To unsubscribe from this group and stop receiving emails from it, send > an > > email to golang-nuts...@googlegroups.com <javascript:>. > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.