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)
|
|
|
|
}
|
|
|
|
}
|
2024-02-24 01:07:51 +01:00
|
|
|
Sink.Add(uint64(n))
|
2023-05-10 21:57:01 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2024-02-24 01:07:51 +01:00
|
|
|
Sink.Add(uint64(n))
|
2023-05-10 21:57:01 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-24 01:07:51 +01:00
|
|
|
var Sink atomic.Uint64
|