Исходники и презентации
This commit is contained in:
31
lessons/slices_and_arrays/dirty_slice/creation_test.go
Normal file
31
lessons/slices_and_arrays/dirty_slice/creation_test.go
Normal file
@ -0,0 +1,31 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// go test -bench=. creation_test.go
|
||||
|
||||
func makeDirty(size int) []byte {
|
||||
var sb strings.Builder
|
||||
sb.Grow(size)
|
||||
|
||||
pointer := unsafe.StringData(sb.String())
|
||||
return unsafe.Slice(pointer, size)
|
||||
}
|
||||
|
||||
var Result []byte
|
||||
|
||||
func BenchmarkMake(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
Result = make([]byte, 0, 10<<20)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkMakeDirty(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
Result = makeDirty(10 << 20)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user