Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package sync_test | 5 package sync_test |
6 | 6 |
7 import ( | 7 import ( |
8 . "sync" | 8 . "sync" |
9 "sync/atomic" | 9 "sync/atomic" |
10 "runtime" | 10 "runtime" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 for i := 0; i < N; i++ { | 33 for i := 0; i < N; i++ { |
34 <-c | 34 <-c |
35 } | 35 } |
36 if *o != 1 { | 36 if *o != 1 { |
37 t.Errorf("once failed: %d is not 1", *o) | 37 t.Errorf("once failed: %d is not 1", *o) |
38 } | 38 } |
39 } | 39 } |
40 | 40 |
41 func BenchmarkOnce(b *testing.B) { | 41 func BenchmarkOnce(b *testing.B) { |
42 const CallsPerSched = 1000 | 42 const CallsPerSched = 1000 |
43 b.SetBytes(1) | |
rsc
2011/06/27 15:59:24
Since this is not processing data, it seems
better
| |
44 procs := runtime.GOMAXPROCS(-1) | 43 procs := runtime.GOMAXPROCS(-1) |
45 N := int32(b.N / CallsPerSched) | 44 N := int32(b.N / CallsPerSched) |
rsc
2011/06/27 15:59:24
Should this be b.N / CallsPerSched / procs?
| |
46 » once := new(Once) | 45 » var once Once |
rsc
2011/06/27 15:59:24
var once Once
| |
47 f := func() {} | 46 f := func() {} |
48 c := make(chan bool, procs) | 47 c := make(chan bool, procs) |
49 for p := 0; p < procs; p++ { | 48 for p := 0; p < procs; p++ { |
50 go func() { | 49 go func() { |
51 for atomic.AddInt32(&N, -1) >= 0 { | 50 for atomic.AddInt32(&N, -1) >= 0 { |
52 runtime.Gosched() | 51 runtime.Gosched() |
53 for g := 0; g < CallsPerSched; g++ { | 52 for g := 0; g < CallsPerSched; g++ { |
54 once.Do(f) | 53 once.Do(f) |
55 } | 54 } |
56 } | 55 } |
57 c <- true | 56 c <- true |
58 }() | 57 }() |
59 } | 58 } |
60 for p := 0; p < procs; p++ { | 59 for p := 0; p < procs; p++ { |
61 <-c | 60 <-c |
62 } | 61 } |
63 } | 62 } |
LEFT | RIGHT |