Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to ericlagergren/decimal #1

Closed
bojanz opened this issue May 29, 2020 · 6 comments
Closed

Switch to ericlagergren/decimal #1

bojanz opened this issue May 29, 2020 · 6 comments

Comments

@bojanz
Copy link
Owner

bojanz commented May 29, 2020

Benchmarks show ericlagergren/decimal to be noticeably faster than cockroachdb/apd, along with doing less allocations. We should try switching.

I'd like to get an answer to ericlagergren/decimal#153 before I merge this, since the current checks feel a bit repetitive.

EDIT: Answer received, waiting on maintainer fix.

@17twenty
Copy link

17twenty commented Oct 3, 2020

Was there any reason to avoid using shopspring/decimal - I've used it a number of times and ended up rolling the start of an accounting library around it. Happy to collab

@bojanz
Copy link
Owner Author

bojanz commented Oct 3, 2020

As the oldest Go decimal library, shopspring/decimal is considered both the least correct and the least performant (in various corner cases). It also wasn't actively developed at the time when I evaluated cockroachdb/apd and ericlagergren/decimal, so it felt natural to skip it.

Since I haven't done this evaluation nor benchmarking myself, I am prepared to be wrong.

Here's the initial discussion where I raised this question: https://www.reddit.com/r/golang/comments/5zkzrq/apd_an_arbitraryprecision_decimal_package/dezc7sv/
The rest was mostly discussions with the community.

@sproutworks
Copy link

There is a new v3 release of cockroachdb/apd. They have done some performance improvements. Where can I find the benchmarks mentioned in this issue?

@bojanz
Copy link
Owner Author

bojanz commented Jan 15, 2022

Okay, so cockroachdb/apd has a new 3.0.0 release with improved performance.

Meanwhile, ericlagergren/decimal's latest release is still from Jan 2nd 2019. It's been a year and a half since I opened this issue, so it's safe to admit that the switch to ericlagergren/decimal can't and won't happen.

Switched our apd usage from v2 to v3 in a4379f5.

Looks like I've managed to lose the original benchmark file I used to compare the two packages. I will try to rebuild it to compare apd v2 and v3 at least, then link to the results here. If someone is curious enough to beat me to it, please go ahead!

@bojanz bojanz closed this as completed Jan 15, 2022
@bojanz
Copy link
Owner Author

bojanz commented Jan 15, 2022

While we wait for currency-specific benchmarks, here's the pi_test from https://github.com/ericlagergren/decimal/tree/master/benchmarks:

With apd v1:

goos: linux
goarch: amd64
pkg: github.com/ericlagergren/decimal/benchmarks
cpu: AMD Ryzen 5 PRO 4650U with Radeon Graphics

BenchmarkPi/foo=ericlagergren_(Go)/prec=9-12  	          148951	      7883 ns/op	     280 B/op	       7 allocs/op
BenchmarkPi/foo=ericlagergren_(Go)/prec=19-12 	           36361	     32957 ns/op	    2481 B/op	      91 allocs/op
BenchmarkPi/foo=ericlagergren_(Go)/prec=38-12 	           16586	     73711 ns/op	    5186 B/op	     185 allocs/op
BenchmarkPi/foo=ericlagergren_(Go)/prec=100-12         	    5455	    215843 ns/op	   18929 B/op	     494 allocs/op

BenchmarkPi/foo=ericlagergren_(GDA)/prec=9-12          	  151893	      7830 ns/op	     176 B/op	       4 allocs/op
BenchmarkPi/foo=ericlagergren_(GDA)/prec=19-12         	   25872	     39272 ns/op	    2969 B/op	     152 allocs/op
BenchmarkPi/foo=ericlagergren_(GDA)/prec=38-12         	   13434	     86877 ns/op	   11990 B/op	     357 allocs/op
BenchmarkPi/foo=ericlagergren_(GDA)/prec=100-12        	    4273	    317912 ns/op	   57518 B/op	    1041 allocs/op

BenchmarkPi/foo=cockroachdb/apd/prec=9-12              	   23175	     50234 ns/op	    7776 B/op	     265 allocs/op
BenchmarkPi/foo=cockroachdb/apd/prec=19-12             	    5770	    189740 ns/op	   19161 B/op	     562 allocs/op
BenchmarkPi/foo=cockroachdb/apd/prec=38-12             	    1804	    751601 ns/op	   46838 B/op	    1187 allocs/op
BenchmarkPi/foo=cockroachdb/apd/prec=100-12            	     205	   5721983 ns/op	  247105 B/op	    4574 allocs/op

BenchmarkPi/foo=shopspring/prec=9-12                   	   24094	     52249 ns/op	   17152 B/op	     753 allocs/op
BenchmarkPi/foo=shopspring/prec=19-12                  	    9541	    117963 ns/op	   43152 B/op	    1804 allocs/op
BenchmarkPi/foo=shopspring/prec=38-12                  	    4465	    238442 ns/op	   95681 B/op	    3773 allocs/op
BenchmarkPi/foo=shopspring/prec=100-12                 	    1785	    731793 ns/op	  282754 B/op	   10084 allocs/op
PASS
ok  	github.com/ericlagergren/decimal/benchmarks	44.797s

With apd v3:

goos: linux
goarch: amd64
pkg: github.com/ericlagergren/decimal/benchmarks
cpu: AMD Ryzen 5 PRO 4650U with Radeon Graphics

BenchmarkPi/foo=ericlagergren_(Go)/prec=9-12  	  153244	      7812 ns/op	     280 B/op	       7 allocs/op
BenchmarkPi/foo=ericlagergren_(Go)/prec=19-12 	   35049	     34490 ns/op	    2481 B/op	      91 allocs/op
BenchmarkPi/foo=ericlagergren_(Go)/prec=38-12 	   16160	     76555 ns/op	    5186 B/op	     185 allocs/op
BenchmarkPi/foo=ericlagergren_(Go)/prec=100-12         	    5262	    220857 ns/op	   18929 B/op	     494 allocs/op

BenchmarkPi/foo=ericlagergren_(GDA)/prec=9-12          	  145813	      8002 ns/op	     176 B/op	       4 allocs/op
BenchmarkPi/foo=ericlagergren_(GDA)/prec=19-12         	   26456	     40685 ns/op	    2969 B/op	     152 allocs/op
BenchmarkPi/foo=ericlagergren_(GDA)/prec=38-12         	   13117	     87714 ns/op	   11990 B/op	     357 allocs/op
BenchmarkPi/foo=ericlagergren_(GDA)/prec=100-12        	    3924	    314365 ns/op	   57519 B/op	    1041 allocs/op

BenchmarkPi/foo=cockroachdb/apd/prec=9-12              	   56811	     20885 ns/op	     128 B/op	       3 allocs/op
BenchmarkPi/foo=cockroachdb/apd/prec=19-12             	    5185	    230889 ns/op	   15975 B/op	     333 allocs/op
BenchmarkPi/foo=cockroachdb/apd/prec=38-12             	     970	   1190979 ns/op	   49721 B/op	    1011 allocs/op
BenchmarkPi/foo=cockroachdb/apd/prec=100-12            	     124	   9376283 ns/op	  234918 B/op	    3941 allocs/op

BenchmarkPi/foo=shopspring/prec=9-12                   	   24312	     47490 ns/op	   17152 B/op	     753 allocs/op
BenchmarkPi/foo=shopspring/prec=19-12                  	   10000	    117907 ns/op	   43152 B/op	    1804 allocs/op
BenchmarkPi/foo=shopspring/prec=38-12                  	    5016	    267262 ns/op	   95680 B/op	    3773 allocs/op
BenchmarkPi/foo=shopspring/prec=100-12                 	    1767	    754493 ns/op	  282754 B/op	   10084 allocs/op
PASS
ok  	github.com/ericlagergren/decimal/benchmarks	44.092s

We can see a drastic reduction of allocations and memory usage, at the expense of CPU time at higher precisions.

@bojanz
Copy link
Owner Author

bojanz commented Jan 21, 2022

Made some benchmarks here: https://gist.github.com/bojanz/eda3c0d6fdc1db16aff0a964121686eb

Copying the results here:

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

I'll also retest once cockroachdb/apd#112 lands in an apd release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants