Skip to content

Instantly share code, notes, and snippets.

@ukautz
Last active February 12, 2020 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukautz/bdc64c680b451200669d78291fcfac02 to your computer and use it in GitHub Desktop.
Save ukautz/bdc64c680b451200669d78291fcfac02 to your computer and use it in GitHub Desktop.
Benchmark T-Digest implementation Golang
package tdigest_benchmark
import (
"fmt"
"math/rand"
"testing"
"time"
ajwerner "github.com/ajwerner/tdigest"
caio "github.com/caio/go-tdigest"
spenczar "github.com/spenczar/tdigest"
)
var (
values []float64
digestAjwerner *ajwerner.TDigest
digestCaio *caio.TDigest
digestSpenczar *spenczar.TDigest
)
func BenchmarkCreate_Ajwerner(b *testing.B) {
for i := 0; i < b.N; i++ {
ajwerner.New()
}
}
func BenchmarkCreate_Caio(b *testing.B) {
for i := 0; i < b.N; i++ {
caio.New()
}
}
func BenchmarkCreate_Spenczar(b *testing.B) {
for i := 0; i < b.N; i++ {
spenczar.New()
}
}
func BenchmarkAdd_Ajwerner(b *testing.B) {
d := ajwerner.New()
for i := 0; i < b.N; i++ {
runBenchmark(d.Record)
}
}
func BenchmarkAdd_Caio(b *testing.B) {
d, _ := caio.New()
for i := 0; i < b.N; i++ {
runBenchmark(func(v float64) {
d.Add(v)
})
}
}
func BenchmarkAdd_Spenczar(b *testing.B) {
d := spenczar.New()
for i := 0; i < b.N; i++ {
runBenchmark(func(v float64) {
d.Add(v, 1)
})
}
}
func BenchmarkQuantile_Ajwerner(b *testing.B) {
for i := 0; i < b.N; i++ {
digestAjwerner.QuantileOf(0.5)
}
}
func BenchmarkQuantile_Caio(b *testing.B) {
for i := 0; i < b.N; i++ {
digestCaio.Quantile(0.5)
}
}
func BenchmarkQuantile_Spenczar(b *testing.B) {
for i := 0; i < b.N; i++ {
digestSpenczar.Quantile(0.5)
}
}
func runBenchmark(add func(float64)) {
for _, v := range values {
add(v)
}
}
func TestComparePercentiles(t *testing.T) {
for _, quantile := range []float64{0.5, 0.75, 0.9, 0.95, 0.99} {
fmt.Printf("# %d percentile\n", int(quantile*100))
fmt.Printf(" from ajwerner: %g\n", digestAjwerner.QuantileOf(quantile))
fmt.Printf(" from caio: %g\n", digestCaio.Quantile(quantile))
fmt.Printf(" from spenczar: %g\n", digestSpenczar.Quantile(quantile))
}
}
func init() {
rand.Seed(time.Now().UnixNano())
values := make([]float64, 10000)
digestAjwerner = ajwerner.New()
digestCaio, _ = caio.New()
digestSpenczar = spenczar.New()
for i := range values {
values[i] = rand.Float64()
digestAjwerner.Record(values[i])
digestCaio.Add(values[i])
digestSpenczar.Add(values[i], 1)
}
}
=== RUN TestComparePercentiles
# 50 percentile
from ajwerner: 0.5028852156914266
from caio: 0.49863161499624364
from spenczar: 0.4991647711124288
# 75 percentile
from ajwerner: 0.7506773697472621
from caio: 0.7492935426008756
from spenczar: 0.7688110569535475
# 90 percentile
from ajwerner: 0.901182182610169
from caio: 0.9001066992693836
from spenczar: 0.8798627020879155
# 95 percentile
from ajwerner: 0.9478999419672552
from caio: 0.9529328402762272
from spenczar: 0.9280995925885083
# 99 percentile
from ajwerner: 0.9910390488563983
from caio: 0.9890282105660144
from spenczar: 0.9827365140977895
--- PASS: TestComparePercentiles (0.00s)
goos: linux
goarch: amd64
pkg: tdigest-benchmark
BenchmarkCreate_Ajwerner-8 495612 2326 ns/op
BenchmarkCreate_Caio-8 58483 19126 ns/op
BenchmarkCreate_Spenczar-8 1000000000 0.976 ns/op
BenchmarkAdd_Ajwerner-8 247815348 5.17 ns/op
BenchmarkAdd_Caio-8 221464058 6.77 ns/op
BenchmarkAdd_Spenczar-8 146966512 6.99 ns/op
BenchmarkQuantile_Ajwerner-8 13528251 91.9 ns/op
BenchmarkQuantile_Caio-8 861026 1232 ns/op
BenchmarkQuantile_Spenczar-8 19665948 54.3 ns/op
PASS
ok tdigest-benchmark 14.577s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment