Created
January 21, 2022 17:58
-
-
Save bojanz/eda3c0d6fdc1db16aff0a964121686eb to your computer and use it in GitHub Desktop.
bojanz/currency benchmark
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 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Results for apd v2:
Results for apd v3: