Files
deep_go/lessons/strings/byte_slice_to_string/comparison_test.go

33 lines
524 B
Go
Raw Permalink Normal View History

package main
import (
"testing"
"unsafe"
)
// go test -bench=. -benchmem comparison_test.go
func Convert(data []byte) string {
if len(data) == 0 {
return ""
}
return unsafe.String(unsafe.SliceData(data), len(data))
}
var Result string
func BenchmarkConvertion(b *testing.B) {
slice := []byte("Hello world!!!")
for i := 0; i < b.N; i++ {
Result = string(slice)
}
}
func BenchmarkUnsafeConvertion(b *testing.B) {
slice := []byte("Hello world!!!")
for i := 0; i < b.N; i++ {
Result = Convert(slice)
}
}