// Sample benchmarks to test which function is better for converting
// an integer into a string. First using the fmt.Sprintf function,
// then the strconv.FormatInt function and then strconv.Itoa.
package listing05_test
import (
"fmt"
"strconv"
"testing"
)
// BenchmarkSprintf provides performance numbers for the
// fmt.Sprintf function.
func BenchmarkSprintf(b *testing.B) {
number := 10
b.ResetTimer()
for i := 0; i < b.N; i++ {
fmt.Sprintf("%d", number)
}
}
// BenchmarkFormat provides performance numbers for the
// strconv.FormatInt function.
func BenchmarkFormat(b *testing.B) {
number := int64(10)
b.ResetTimer()
for i := 0; i < b.N; i++ {
strconv.FormatInt(number, 10)
}
}
// BenchmarkItoa provides performance numbers for the
// strconv.Itoa function.
func BenchmarkItoa(b *testing.B) {
number := 10
b.ResetTimer()
for i := 0; i < b.N; i++ {
strconv.Itoa(number)
}
}
$w
top of page
Dress To Impress
Productivity Crash Course
5днів5 днів
555 5
Учасники, які виконали всі завдання, отримають бейджі після завершення онлайн-курсу.
Про онлайн-курс
Stressed at work, overwhelmed and not getting everything done? This quick productivity course will teach you the skills you need to get back on track.
1. Manage your time
2. Work with laser focus
3. Get more done without the stress
Ви також можете приєднатись до цього онлайн-курсу з мобільного додатка. До додатка