34 lines
574 B
Go
Raw Normal View History

2016-10-23 04:15:47 +00:00
package main
import (
"testing"
"unsafe"
)
var code = "Hello World"
var slice = []byte("Hello World 2")
func BenchmarkStringToBytesSafe(b *testing.B) {
for i := 0; i < b.N; i++ {
slice = []byte(code)
}
}
func BenchmarkStringToBytesUnsafe(b *testing.B) {
for i := 0; i < b.N; i++ {
slice = *(*[]byte)(unsafe.Pointer(&code))
}
}
func BenchmarkBytesToStringSafe(b *testing.B) {
for i := 0; i < b.N; i++ {
code = string(slice)
}
}
func BenchmarkBytesToStringUnsafe(b *testing.B) {
for i := 0; i < b.N; i++ {
code = *(*string)(unsafe.Pointer(&slice))
}
}