forked from hoanhan101/ultimate-go
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sub_test.go
45 lines (34 loc) · 909 Bytes
/
sub_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// -------------
// Sub benchmark
// -------------
// Like sub test, we can also do sub benchmark.
// Sample available commands:
// go test -run none -bench . -benchtime 3s -benchmem
// go test -run none -bench BenchmarkSprintSub/none -benchtime 3s -benchmem
// go test -run none -bench BenchmarkSprintSub/format -benchtime 3s -benchmem
package main
import (
"fmt"
"testing"
)
// BenchmarkSprint tests all the Sprint related benchmarks as sub benchmarks.
func BenchmarkSprintSub(b *testing.B) {
b.Run("none", benchSprint)
b.Run("format", benchSprintf)
}
// benchSprint tests the performance of using Sprint.
func benchSprint(b *testing.B) {
var s string
for i := 0; i < b.N; i++ {
s = fmt.Sprint("hello")
}
gs = s
}
// benchSprintf tests the performance of using Sprintf.
func benchSprintf(b *testing.B) {
var s string
for i := 0; i < b.N; i++ {
s = fmt.Sprintf("hello")
}
gs = s
}