Skip to content

Commit

Permalink
add benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
sgreben committed Dec 6, 2023
1 parent da1e55c commit 6bce2e9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ A tiny library for linear interpolation. `O(log(N))` per evaluation for `N` cont
import "github.com/sgreben/piecewiselinear"
```

- [Get it](#get-it)
- [Use it](#use-it)
- [Benchmarks](#benchmarks)


## Get it

```sh
Expand Down Expand Up @@ -35,3 +40,25 @@ func main() {
// 0 0.5 1 0.5 0 0 0
}
```

## Benchmarks

On an Apple M1 Pro:

- **6ns** per evaluation (`.At(x)`) for 10 control points
- **320ns** per evaluation for 10 million control points.

```
goos: darwin
goarch: arm64
pkg: github.com/sgreben/piecewiselinear
BenchmarkAt4-10 217302646 5.461 ns/op 0 B/op 0 allocs/op
BenchmarkAt8-10 197175420 6.048 ns/op 0 B/op 0 allocs/op
BenchmarkAt10-10 188384818 6.283 ns/op 0 B/op 0 allocs/op
BenchmarkAt100-10 138276301 9.086 ns/op 0 B/op 0 allocs/op
BenchmarkAt1k-10 41258203 25.18 ns/op 0 B/op 0 allocs/op
BenchmarkAt10k-10 16852758 69.99 ns/op 0 B/op 0 allocs/op
BenchmarkAt100k-10 11745384 100.5 ns/op 0 B/op 0 allocs/op
BenchmarkAt1M-10 8501438 143.0 ns/op 0 B/op 0 allocs/op
BenchmarkAt10M-10 3659188 319.5 ns/op 0 B/op 0 allocs/op
```
54 changes: 54 additions & 0 deletions piecewiselinear_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package piecewiselinear

import (
"fmt"
"math/rand"
"testing"
)

Expand Down Expand Up @@ -324,3 +325,56 @@ func TestFunction_IsInterpolatedAt(t *testing.T) {
})
}
}

func benchmarkWithKPoints(b *testing.B, k int) {
var f Function
f.Y = make([]float64, k)
for i := range f.Y {
f.Y[i] = rand.Float64()
}
f.X = Span(0, 1, len(f.Y))
xs := make([]float64, len(f.X))
for i := range f.X {
xs[i] = rand.Float64()
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = f.At(xs[n%len(f.X)])
}
}

func BenchmarkAt4(b *testing.B) {
benchmarkWithKPoints(b, 4)
}

func BenchmarkAt8(b *testing.B) {
benchmarkWithKPoints(b, 8)
}

func BenchmarkAt10(b *testing.B) {
benchmarkWithKPoints(b, 10)
}

func BenchmarkAt100(b *testing.B) {
benchmarkWithKPoints(b, 100)
}

func BenchmarkAt1k(b *testing.B) {
benchmarkWithKPoints(b, 1_000)
}

func BenchmarkAt10k(b *testing.B) {
benchmarkWithKPoints(b, 10_000)
}

func BenchmarkAt100k(b *testing.B) {
benchmarkWithKPoints(b, 100_000)
}

func BenchmarkAt1M(b *testing.B) {
benchmarkWithKPoints(b, 1_000_000)
}

func BenchmarkAt10M(b *testing.B) {
benchmarkWithKPoints(b, 10_000_000)
}

0 comments on commit 6bce2e9

Please sign in to comment.