Skip to content

Instantly share code, notes, and snippets.

@ymotongpoo

ymotongpoo/benchstat

Last active Aug 23, 2019
Embed
What would you like to do?
testing.B.ReportMetric
% benchstat bubble.txt selection.txt
name old time/op new time/op delta
Sort-4 29.4ns ± 1% 29.1ns ± 0% -1.00% (p=0.016 n=5+4)
name old compares/op new compares/op delta
Sort-4 15.0 ± 0% 15.0 ± 0% ~ (all equal)
name old alloc/op new alloc/op delta
Sort-4 0.00B 0.00B ~ (all equal)
name old allocs/op new allocs/op delta
Sort-4 0.00 0.00 ~ (all equal)
goos: linux
goarch: amd64
BenchmarkSort-4 40962218 29.3 ns/op 15.0 compares/op 0 B/op 0 allocs/op
BenchmarkSort-4 41243956 29.6 ns/op 15.0 compares/op 0 B/op 0 allocs/op
BenchmarkSort-4 40784164 29.6 ns/op 15.0 compares/op 0 B/op 0 allocs/op
BenchmarkSort-4 40886186 29.4 ns/op 15.0 compares/op 0 B/op 0 allocs/op
BenchmarkSort-4 41580344 29.2 ns/op 15.0 compares/op 0 B/op 0 allocs/op
PASS
ok _/usr/local/google/home/yoshifumi/tmp/bench 10.884s
package foo
func bubbleSort(s []int, compares *int) []int {
for i := 0; i < len(s)-1; i++ {
for j := i + 1; j < len(s); j++ {
*compares++
if s[i] > s[j] {
s[i], s[j] = s[j], s[i]
}
}
}
return s
}
func selectionSort(s []int, compares *int) []int {
for i := 0; i < len(s)-1; i++ {
pos := i
for j := i + 1; j < len(s); j++ {
*compares++
if s[pos] > s[j] {
pos = j
}
}
s[i], s[pos] = s[pos], s[i]
}
return s
}
func sort(s []int, compares *int) []int {
// return bubbleSort(s, compares)
return selectionSort(s, compares)
}
package foo
import "testing"
func TestSort(t *testing.T) {
in := []int{3, 2, 1, 4, 6, 5}
want := []int{1, 2, 3, 4, 5, 6}
var n int
out := sort(in, &n)
for i, w := range want {
if out[i] != w {
t.Errorf("want: %v, out: %v", w, out[i])
}
}
}
func BenchmarkSort(b *testing.B) {
var compares int
for i := 0; i < b.N; i++ {
sort([]int{3, 5, 1, 2, 6, 4}, &compares)
}
b.ReportMetric(float64(compares)/float64(b.N), "compares/op")
}
goos: linux
goarch: amd64
BenchmarkSort-4 40892257 29.2 ns/op 15.0 compares/op 0 B/op 0 allocs/op
BenchmarkSort-4 41086053 29.1 ns/op 15.0 compares/op 0 B/op 0 allocs/op
BenchmarkSort-4 40863766 28.5 ns/op 15.0 compares/op 0 B/op 0 allocs/op
BenchmarkSort-4 41608278 29.1 ns/op 15.0 compares/op 0 B/op 0 allocs/op
BenchmarkSort-4 39724590 29.1 ns/op 15.0 compares/op 0 B/op 0 allocs/op
PASS
ok _/usr/local/google/home/yoshifumi/tmp/bench 9.908s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.