2023-05-10 19:57:01 +00: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-10 22:53:58 +00:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2023-05-10 19:57:01 +00:00
|
|
|
n := 0
|
|
|
|
for pb.Next() {
|
|
|
|
for i := range buf {
|
|
|
|
s := ToUnsafeString(buf[:i])
|
|
|
|
n += len(s)
|
|
|
|
}
|
|
|
|
}
|
2024-02-24 00:07:51 +00:00
|
|
|
Sink.Add(uint64(n))
|
2023-05-10 19:57:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkToUnsafeBytes(b *testing.B) {
|
|
|
|
s := "foobarbaz abcde fafdsfds"
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.SetBytes(int64(len(s)))
|
2023-05-10 22:53:58 +00:00
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
2023-05-10 19:57:01 +00:00
|
|
|
n := 0
|
|
|
|
for pb.Next() {
|
|
|
|
for i := range s {
|
|
|
|
s := ToUnsafeBytes(s[:i])
|
|
|
|
n += len(s)
|
|
|
|
}
|
|
|
|
}
|
2024-02-24 00:07:51 +00:00
|
|
|
Sink.Add(uint64(n))
|
2023-05-10 19:57:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-24 00:07:51 +00:00
|
|
|
var Sink atomic.Uint64
|