Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0577ba8

Browse files
20220423
1 parent ab87189 commit 0577ba8

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

‎README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ ps:白天上班,晚上更新,尽量日更,比心
7373

7474
[第32章 单元测试](https://github.com/java-aodeng/golang-examples/blob/master/go-32/function_test.go)
7575

76-
第33章 Benchmark
76+
[第33章 Benchmark](https://github.com/java-aodeng/golang-examples/blob/master/go-33/benchmark_test.go)
7777

7878
第34章 BDD
7979

‎go-33/benchmark_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package go_33
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
)
7+
8+
/*
9+
Benchmark
10+
用于一些性能测试中,方法名字以Benchmark为前缀
11+
参数使用testing.B
12+
13+
func BenchmarkConcatStringByBytesBuffer(b *testing.B) {
14+
//与性能测试无关代码
15+
b.ResetTimer()
16+
for i := 0; i < b.N; i++ {
17+
//测试代码
18+
}
19+
b.StopTimer()
20+
//与性能测试无关代码
21+
}
22+
23+
*/
24+
25+
func BenchmarkConcatStringByBytesBuffer(b *testing.B) {
26+
elems := []string{"1", "2", "3", "4", "5"}
27+
b.ResetTimer()
28+
for i := 0; i < b.N; i++ {
29+
var buf bytes.Buffer
30+
31+
for _, elem := range elems {
32+
buf.WriteString(elem)
33+
34+
}
35+
}
36+
b.StopTimer()
37+
38+
}
39+
40+
/*
41+
//运行结果
42+
goos: windows
43+
goarch: amd64
44+
BenchmarkConcatStringByBytesBuffer-4 10000000 142 ns/op
45+
PASS*/

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /