go-countlines/benchmark_test.go

61 lines
964 B
Go

package countlines
import (
"math/rand"
"testing"
)
type size struct {
name string
l int
}
var sizes = []size{
{"32", 32},
{"128", 128},
{"1K", 1 * 1024},
{"16K", 16 * 1024},
{"128K", 128 * 1024},
{"1M", 1024 * 1024},
{"16M", 16 * 1024 * 1024},
{"128M", 128 * 1024 * 1024},
{"512M", 512 * 1024 * 1024},
}
func randRead64(s []uint64) {
for i := range s {
s[i] = uint64(rand.Int63())
}
}
func BenchmarkCountNewlines(b *testing.B) {
for _, size := range sizes {
b.Run(size.name, func(b *testing.B) {
s := make([]byte, size.l)
rand.Read(s)
b.SetBytes(int64(size.l))
b.ResetTimer()
for i := 0; i < b.N; i++ {
CountNewlines(s)
}
})
}
}
func BenchmarkCountNewlinesGo(b *testing.B) {
for _, size := range sizes {
b.Run(size.name, func(b *testing.B) {
s := make([]byte, size.l)
rand.Read(s)
b.SetBytes(int64(size.l))
b.ResetTimer()
for i := 0; i < b.N; i++ {
countNewlinesGo(s)
}
})
}
}