22 lines
367 B
Go
22 lines
367 B
Go
|
|
package main
|
||
|
|
|
||
|
|
import "testing"
|
||
|
|
|
||
|
|
// go test -bench=. performance_test.go
|
||
|
|
|
||
|
|
func BenchmarkWithSlice(b *testing.B) {
|
||
|
|
slice := []int{1, 2, 3}
|
||
|
|
for i := 0; i < b.N; i++ {
|
||
|
|
value := slice[1]
|
||
|
|
slice[1] = value
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func BenchmarkWithHashTable(b *testing.B) {
|
||
|
|
table := map[int]int{0: 1, 1: 2, 2: 3}
|
||
|
|
for i := 0; i < b.N; i++ {
|
||
|
|
value := table[1]
|
||
|
|
table[1] = value
|
||
|
|
}
|
||
|
|
}
|