Last active
May 2, 2018 09:19
-
-
Save narqo/98be75e681186aa17dae8356c6a44a72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
goos: darwin | |
goarch: amd64 | |
pkg: test-sort | |
BenchmarkSort/setsize_10/sort-4 5000000 295 ns/op 32 B/op 1 allocs/op | |
BenchmarkSort/setsize_10/slice-4 3000000 598 ns/op 112 B/op 3 allocs/op | |
BenchmarkSort/setsize_100/sort-4 200000 8279 ns/op 32 B/op 1 allocs/op | |
BenchmarkSort/setsize_100/slice-4 100000 13276 ns/op 112 B/op 3 allocs/op | |
BenchmarkSort/setsize_1000/sort-4 10000 165268 ns/op 32 B/op 1 allocs/op | |
BenchmarkSort/setsize_1000/slice-4 10000 209613 ns/op 112 B/op 3 allocs/op | |
BenchmarkSort/setsize_10000/sort-4 1000 2226969 ns/op 32 B/op 1 allocs/op | |
BenchmarkSort/setsize_10000/slice-4 500 2716022 ns/op 112 B/op 3 allocs/op | |
BenchmarkSort/setsize_100000/sort-4 50 28135270 ns/op 32 B/op 1 allocs/op | |
BenchmarkSort/setsize_100000/slice-4 50 33965116 ns/op 112 B/op 3 allocs/op | |
PASS | |
ok test-sort 18.454s |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"sort" | |
"testing" | |
"time" | |
) | |
type datum struct { | |
time time.Time | |
} | |
type datums []datum | |
func (d datums) Len() int { | |
return len(d) | |
} | |
func (d datums) Less(i, j int) bool { | |
return d[i].time.Before(d[j].time) | |
} | |
func (d datums) Swap(i, j int) { | |
d[i], d[j] = d[j], d[i] | |
} | |
var dataset datums | |
func BenchmarkSort(b *testing.B) { | |
rand.Seed(42) | |
now := time.Now().Unix() | |
for i := 10; i < 1e6; i = i * 10 { | |
b.Run(fmt.Sprintf("setsize %d", i), func(b *testing.B) { | |
testdata := make([]datum, 0, i) | |
for i := 0; i < cap(testdata); i++ { | |
d := datum{time.Unix(rand.Int63n(now), 0)} | |
testdata = append(testdata, d) | |
} | |
dataset = make(datums, i) | |
b.ResetTimer() | |
benchSort(b, dataset, testdata) | |
}) | |
} | |
} | |
func benchSort(b *testing.B, dataset, testdata datums) { | |
b.Run("sort", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
copy(dataset, testdata) | |
sort.Sort(dataset) | |
} | |
}) | |
b.Run("slice", func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
copy(dataset, testdata) | |
sort.Slice(dataset, func(i, j int) bool { | |
return dataset[i].time.Before(dataset[j].time) | |
}) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment