2023-05-10 21:57:01 +02:00
|
|
|
package bytesutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkToUnsafeString(b *testing.B) {
|
|
|
|
buf := []byte("foobarbaz abcde fafdsfds")
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(int64(len(buf)))
|
2023-05-11 00:53:58 +02:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2023-05-10 21:57:01 +02:00
|
|
|
n := 0
|
|
|
|
for pb.Next() {
|
|
|
|
for i := range buf {
|
|
|
|
s := ToUnsafeString(buf[:i])
|
|
|
|
n += len(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
atomic.AddUint64(&Sink, uint64(n))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkToUnsafeBytes(b *testing.B) {
|
|
|
|
s := "foobarbaz abcde fafdsfds"
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(int64(len(s)))
|
2023-05-11 00:53:58 +02:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2023-05-10 21:57:01 +02:00
|
|
|
n := 0
|
|
|
|
for pb.Next() {
|
|
|
|
for i := range s {
|
|
|
|
s := ToUnsafeBytes(s[:i])
|
|
|
|
n += len(s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
atomic.AddUint64(&Sink, uint64(n))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var Sink uint64
|