Skip to content

Commit 0affa29

Browse files
Adjusted benchmark code.
1 parent 57f2d4a commit 0affa29

2 files changed

Lines changed: 36 additions & 71 deletions

File tree

‎chapter9/listing05/listing05.go‎

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,46 @@
1-
// go test -run=none -bench=.
1+
// Benchmark tests contains three benchmark tests for converting
2+
// an integer into a string. First using the fmt.Sprintf function,
3+
// then the strconv.FormatInt function and then strconv.Itoa.
4+
package listing05_test
25

3-
// Sample test to show how to write benchmark tests.
4-
package listing05
6+
import (
7+
"fmt"
8+
"strconv"
9+
"testing"
10+
)
511

6-
import "testing"
12+
// BenchmarkSprintf provides performance numbers for the
13+
// fmt.Sprintf function.
14+
func BenchmarkSprintf(b *testing.B) {
15+
number := 10
16+
17+
b.ResetTimer()
718

8-
// BenchmarkRowTraverse capture the time it takes to perform
9-
// a row traversal.
10-
func BenchmarkRowTraverse(b *testing.B) {
1119
for i := 0; i < b.N; i++ {
12-
rowTraverse()
20+
fmt.Sprintf("%d", number)
1321
}
1422
}
1523

16-
// BenchmarkColTraverse capture the time it takes to perform
17-
// a column traversal.
18-
func BenchmarkColTraverse(b *testing.B) {
24+
// BenchmarkFormat provides performance numbers for the
25+
// strconv.FormatInt function.
26+
func BenchmarkFormat(b *testing.B) {
27+
number := int64(10)
28+
29+
b.ResetTimer()
30+
31+
for i := 0; i < b.N; i++ {
32+
strconv.FormatInt(number, 10)
33+
}
34+
}
35+
36+
// BenchmarkItoa provides performance numbers for the
37+
// strconv.Itoa function.
38+
func BenchmarkItoa(b *testing.B) {
39+
number := 10
40+
41+
b.ResetTimer()
42+
1943
for i := 0; i < b.N; i++ {
20-
colTraverse()
44+
strconv.Itoa(number)
2145
}
2246
}

0 commit comments

Comments
 (0)