Skip to content

Instantly share code, notes, and snippets.

@odeke-em
Created February 6, 2018 07:27
Show Gist options
  • Save odeke-em/532c14ab67d71c9c0b95518a7a526058 to your computer and use it in GitHub Desktop.
Save odeke-em/532c14ab67d71c9c0b95518a7a526058 to your computer and use it in GitHub Desktop.
package it
import "testing"
func BenchmarkMakeReslicingN10(b *testing.B) {
benchmarkMakeReslicing(b, 10)
}
func BenchmarkMakeReslicingN100(b *testing.B) {
benchmarkMakeReslicing(b, 100)
}
func BenchmarkMakeReslicingN1000(b *testing.B) {
benchmarkMakeReslicing(b, 1000)
}
func BenchmarkMakeReslicingN10000(b *testing.B) {
benchmarkMakeReslicing(b, 10000)
}
func BenchmarkMakeReslicingN100000(b *testing.B) {
benchmarkMakeReslicing(b, 100000)
}
func benchmarkMakeReslicing(b *testing.B, n int) {
b.ResetTimer()
sl := make([]string, 0, n)
for i := 0; i < b.N; i++ {
sl = make([]string, n)
}
if len(sl) != n {
b.Fatalf("Expecting len(s) = %d", n)
}
b.ReportAllocs()
}
func BenchmarkSliceClearingIdiom10(b *testing.B) {
benchmarkSliceClearingIdiom(b, 10)
}
func BenchmarkSliceClearingIdiom100(b *testing.B) {
benchmarkSliceClearingIdiom(b, 100)
}
func BenchmarkSliceClearingIdiom1000(b *testing.B) {
benchmarkSliceClearingIdiom(b, 1000)
}
func BenchmarkSliceClearingIdiom10000(b *testing.B) {
benchmarkSliceClearingIdiom(b, 10000)
}
func BenchmarkSliceClearingIdiom100000(b *testing.B) {
benchmarkSliceClearingIdiom(b, 100000)
}
func benchmarkSliceClearingIdiom(b *testing.B, n int) {
b.ResetTimer()
sl := make([]string, n)
for i := 0; i < b.N; i++ {
sl = sl[:0]
}
if cap(sl) != n {
b.Fatalf("Expecting len(s) = %d", n)
}
b.ReportAllocs()
}
$ benchstat old.txt new.txt 
name       old time/op    new time/op    delta
N10-4        88.9ns ± 1%     0.4ns ± 1%   -99.60%  (p=0.029 n=4+4)
N100-4        476ns ±11%       0ns ± 1%   -99.93%  (p=0.029 n=4+4)
N1000-4      3.63µs ± 1%    0.00µs ± 1%   -99.99%  (p=0.029 n=4+4)
N10000-4     31.8µs ± 1%     0.0µs ± 2%  -100.00%  (p=0.029 n=4+4)
N100000-4     317µs ± 1%       0µs ± 6%  -100.00%  (p=0.029 n=4+4)

name       old alloc/op   new alloc/op   delta
N10-4          160B ± 0%       0B ±NaN%  -100.00%  (p=0.029 n=4+4)
N100-4       1.79kB ± 0%   0.00kB ±NaN%  -100.00%  (p=0.029 n=4+4)
N1000-4      16.4kB ± 0%    0.0kB ±NaN%  -100.00%  (p=0.029 n=4+4)
N10000-4      164kB ± 0%      0kB ±NaN%  -100.00%  (p=0.029 n=4+4)
N100000-4    1.61MB ± 0%   0.00MB ±NaN%  -100.00%  (p=0.029 n=4+4)

name       old allocs/op  new allocs/op  delta
N10-4          1.00 ± 0%     0.00 ±NaN%  -100.00%  (p=0.029 n=4+4)
N100-4         1.00 ± 0%     0.00 ±NaN%  -100.00%  (p=0.029 n=4+4)
N1000-4        1.00 ± 0%     0.00 ±NaN%  -100.00%  (p=0.029 n=4+4)
N10000-4       1.00 ± 0%     0.00 ±NaN%  -100.00%  (p=0.029 n=4+4)
N100000-4      1.00 ± 0%     0.00 ±NaN%  -100.00%  (p=0.029 n=4+4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment