-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathbench_test.go
53 lines (40 loc) · 1.11 KB
/
bench_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
46
47
48
49
50
51
52
53
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// go test -run none -bench . -benchtime 3s -benchmem
// Write three benchmark tests for converting an integer into a string. First using the
// fmt.Sprintf function, then the strconv.FormatInt function and then strconv.Itoa.
// Identify which function performs the best.
package main
import (
"fmt"
"strconv"
"testing"
)
var fs string
// BenchmarkSprintf provides performance numbers for the fmt.Sprintf function
func BenchmarkSprintf(b *testing.B) {
number := 10
var s string
for i := 0; i < b.N; i++ {
s = fmt.Sprintf("%d", number)
}
fs = s
}
// BenchmarkFormat provides performance numbers for the strconv.FormatInt function
func BenchmarkFormat(b *testing.B) {
number := int64(10)
var s string
for i := 0; i < b.N; i++ {
s = strconv.FormatInt(number, 10)
}
fs = s
}
// BenchmarkItoa provides performance numbers for the strconv.Itoa function
func BenchmarkItoa(b *testing.B) {
number := 10
var s string
for i := 0; i < b.N; i++ {
s = strconv.Itoa(number)
}
fs = s
}