0%

go单元测试之性能测试

一、 创建 **_test 文件

二、 输入 demo

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
package main

import (
"fmt"
"strconv"
"testing"
)

func BenchmarkSprintf(b *testing.B) {
num := 10
b.ResetTimer()
for i := 0; i < b.N; i++ {
fmt.Sprintf("%d", num)
}
}

func BenchmarkFormat(b *testing.B) {
num := int64(10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
strconv.FormatInt(num, 10)
}
}

func BenchmarkItoa(b *testing.B) {
num := 10
b.ResetTimer()
for i := 0; i < b.N; i++ {
strconv.Itoa(num)
}
}

三、执行 go test -bench=. -run=none

四、结果

1
2
3
4
5
6
7
8
goos: windows
goarch: amd64
pkg: hexo
BenchmarkSprintf-6 14150125 83.5 ns/op
BenchmarkFormat-6 412018723 2.89 ns/op
BenchmarkItoa-6 413478573 2.90 ns/op
PASS
ok hexo 4.435s

五、文章参考

  • https://blog.csdn.net/qq_40876291/article/details/84336319