Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package main
import (
"math/rand"
"sort"
"testing"
)
func BenchmarkInsertSorted(b *testing.B) {
data := make([]int, b.N)
for i := 0; i < b.N; i++ {
data[i] = rand.Int()
}
a := []int{}
for z := 0; z < len(data); z++ {
x := data[z]
i := sort.SearchInts(a, x)
if i >= len(a) || a[i] != x {
a = append(a, 0)
copy(a[i+1:], a[i:])
a[i] = x
}
}
}
func BenchmarkSortAfter(b *testing.B) {
data := make([]int, b.N)
for i := 0; i < b.N; i++ {
data[i] = rand.Int()
}
a := []int{}
for z := 0; z < len(data); z++ {
x := data[z]
a = append(a, x)
}
sort.Ints(a)
j := 0
for i := 1; i < len(a); i++ {
if a[j] == a[i] {
continue
}
j++
a[j] = a[i]
}
a = a[:j+1]
}
@apiarian

This comment has been minimized.

Copy link
Owner Author

commented Feb 18, 2019

go test -bench . -benchtime 100ms
goos: darwin
goarch: amd64
pkg: github.com/apiarian/stream-sort
BenchmarkInsertSorted-8           100000             11811 ns/op
BenchmarkSortAfter-8              500000               351 ns/op
PASS
ok      github.com/apiarian/stream-sort 1.384s
go test -bench . -benchtime 200ms
goos: darwin
goarch: amd64
pkg: github.com/apiarian/stream-sort
BenchmarkInsertSorted-8           300000             38754 ns/op
BenchmarkSortAfter-8             1000000               340 ns/op
PASS
ok      github.com/apiarian/stream-sort 11.995s
@apiarian

This comment has been minimized.

Copy link
Owner Author

commented Feb 18, 2019

Yup, don't do this. Leave the sorting to the last possible moment if you can. Maybe make a write-ahead-log or something.

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.