Skip to content

Instantly share code, notes, and snippets.

@bojanz
Created January 21, 2022 17:58
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 bojanz/eda3c0d6fdc1db16aff0a964121686eb to your computer and use it in GitHub Desktop.
Save bojanz/eda3c0d6fdc1db16aff0a964121686eb to your computer and use it in GitHub Desktop.
bojanz/currency benchmark
package currency_test
import (
"testing"
"github.com/bojanz/currency"
)
var result currency.Amount
var cmpResult int
func BenchmarkAdd(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
y, _ := currency.NewAmount("12.99", "USD")
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = x.Add(y)
}
result = z
}
func BenchmarkSub(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
y, _ := currency.NewAmount("12.99", "USD")
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = x.Sub(y)
}
result = z
}
func BenchmarkMul(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = x.Mul("2")
}
result = z
}
func BenchmarkMulDec(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = x.Mul("2.5")
}
result = z
}
func BenchmarkDiv(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = x.Div("2")
}
result = z
}
func BenchmarkDivDec(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
var z currency.Amount
for n := 0; n < b.N; n++ {
z, _ = x.Div("2.5")
}
result = z
}
func BenchmarkRound(b *testing.B) {
x, _ := currency.NewAmount("34.9876", "USD")
var z currency.Amount
for n := 0; n < b.N; n++ {
z = x.Round()
}
result = z
}
func BenchmarkCmp(b *testing.B) {
x, _ := currency.NewAmount("34.99", "USD")
y, _ := currency.NewAmount("12.99", "USD")
var z int
for n := 0; n < b.N; n++ {
z, _ = x.Cmp(y)
}
cmpResult = z
}
@bojanz
Copy link
Author

bojanz commented Jan 21, 2022

Results for apd v2:

BenchmarkAdd-12       	 2718600	       435.0 ns/op	     128 B/op	       3 allocs/op
BenchmarkSub-12       	 3016879	       405.6 ns/op	      88 B/op	       3 allocs/op
BenchmarkMul-12       	 1447600	       820.8 ns/op	     168 B/op	       5 allocs/op
BenchmarkMulDec-12       1000000	      1055 ns/op	     184 B/op	       7 allocs/op
BenchmarkDiv-12       	  465312	      2928 ns/op	     336 B/op	      11 allocs/op
BenchmarkDivDec-12    	  355858	      3116 ns/op	     352 B/op	      13 allocs/op
BenchmarkRound-12     	  870912	      1228 ns/op	     296 B/op	      11 allocs/op
BenchmarkCmp-12       	85437470	        15.05 ns/op	       0 B/op	       0 allocs/op

Results for apd v3:

BenchmarkAdd-12       	 4333795	       286.2 ns/op	      64 B/op	       2 allocs/op
BenchmarkSub-12       	 4126153	       298.8 ns/op	      64 B/op	       2 allocs/op
BenchmarkMul-12       	 1990543	       625.5 ns/op	      96 B/op	       3 allocs/op
BenchmarkMulDec-12      1366653	       863.4 ns/op	     112 B/op	       5 allocs/op
BenchmarkDiv-12       	  627897	      1614 ns/op	      96 B/op	       3 allocs/op
BenchmarkDivDec-12    	  889832	      1317 ns/op	     112 B/op	       5 allocs/op
BenchmarkRound-12     	 2020036	       574.0 ns/op	      64 B/op	       2 allocs/op
BenchmarkCmp-12       	84142538	        14.26 ns/op	       0 B/op	       0 allocs/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment