Исходники и презентации
This commit is contained in:
30
lessons/strings/allocations/main.go
Normal file
30
lessons/strings/allocations/main.go
Normal file
@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var result string
|
||||
|
||||
func Concat(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
result = "Hello" + " world"
|
||||
}
|
||||
}
|
||||
|
||||
func Conversion(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
result = string([]byte("Hello world"))
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
b1 := testing.Benchmark(Concat)
|
||||
fmt.Println("b1:", b1.AllocsPerOp()) // 0
|
||||
fmt.Println("b1:", b1.AllocedBytesPerOp()) // 0
|
||||
|
||||
b2 := testing.Benchmark(Conversion)
|
||||
fmt.Println("b2:", b2.AllocsPerOp()) // 1
|
||||
fmt.Println("b2:", b2.AllocedBytesPerOp()) // 16
|
||||
}
|
||||
Reference in New Issue
Block a user