-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8890931
commit dd12918
Showing
3 changed files
with
86 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// MIT License | ||
// Copyright (c) 2020 Qi Yin <[email protected]> | ||
|
||
package benchmark | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
"unsafe" | ||
|
||
"github.com/thinkeridea/go-extend/exsync" | ||
) | ||
|
||
type one int | ||
|
||
func (o *one) Increment() { | ||
*o++ | ||
} | ||
|
||
func BenchmarkSyncOnce(b *testing.B) { | ||
var once sync.Once | ||
f := func() { | ||
_ = new(one) | ||
} | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
once.Do(f) | ||
} | ||
}) | ||
} | ||
|
||
func BenchmarkOnce(b *testing.B) { | ||
var once exsync.Once | ||
f := func() interface{} { return new(one) } | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
_ = once.Do(f).(*one) | ||
} | ||
}) | ||
} | ||
|
||
func BenchmarkOncePointer(b *testing.B) { | ||
var once exsync.OncePointer | ||
f := func() unsafe.Pointer { return unsafe.Pointer(new(one)) } | ||
b.RunParallel(func(pb *testing.PB) { | ||
for pb.Next() { | ||
_ = (*one)(once.Do(f)) | ||
} | ||
}) | ||
} |
This file contains 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
This file contains 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