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

conversion: add PrettyDec, optimize Dec, add benchmarks and fuzzing #130

Merged
merged 15 commits into from
Mar 22, 2023

Conversation

holiman
Copy link
Owner

@holiman holiman commented Mar 21, 2023

This PR implements the Dec method 'natively', and adds the API-method PrettyDec.

// Dec returns the decimal representation of z.
func (z *Int) Dec() string

// PrettyDec returns the decimal representation of z, with thousands-separators.
func (z *Int) PrettyDec(separator byte) string 

It also adds benchmarks and a new fuzz-test for the changed methods.

BenchmarkDecimal/ToDecimal/uint256-8         	   12324	    102673 ns/op	   11344 B/op	     248 allocs/op
BenchmarkDecimal/ToPrettyDecimal/uint256-8   	    9595	    110235 ns/op	   14720 B/op	     251 allocs/op
BenchmarkDecimal/ToDecimal/big-8             	   10000	    133930 ns/op	   31920 B/op	     594 allocs/op

@codecov
Copy link

codecov bot commented Mar 21, 2023

Codecov Report

Merging #130 (e637640) into master (87b9142) will not change coverage.
The diff coverage is 100.00%.

❗ Current head e637640 differs from pull request most recent head 46c05a7. Consider uploading reports for the commit 46c05a7 to get more accurate results

@@            Coverage Diff            @@
##            master      #130   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines         1543      1597   +54     
=========================================
+ Hits          1543      1597   +54     

@holiman
Copy link
Owner Author

holiman commented Mar 21, 2023

The 'native' implementation is better on allocs, but way slower

BenchmarkDecimal/ToDecimal/uint256-8         	    1590	    745711 ns/op	   11344 B/op	     248 allocs/op
BenchmarkDecimal/ToPrettyDecimal/uint256-8   	    2836	    749668 ns/op	   14720 B/op	     251 allocs/op
BenchmarkDecimal/ToDecimal/big-8             	    9585	    144120 ns/op	   31920 B/op	     594 allocs/op

@holiman
Copy link
Owner Author

holiman commented Mar 22, 2023

Now it looks beter:

goos: linux
goarch: amd64
pkg: github.com/holiman/uint256
cpu: 12th Gen Intel(R) Core(TM) i7-1270P
BenchmarkDecimal/ToDecimal/uint256-8         	   10176	    126366 ns/op	   11344 B/op	     248 allocs/op
BenchmarkDecimal/ToPrettyDecimal/uint256-8   	    6853	    155594 ns/op	   40784 B/op	     754 allocs/op
BenchmarkDecimal/ToDecimal/big-8             	   10000	    147628 ns/op	   31920 B/op	     594 allocs/op
PASS
ok  	github.com/holiman/uint256	5.801s

@holiman holiman marked this pull request as ready for review March 22, 2023 10:21
@holiman
Copy link
Owner Author

holiman commented Mar 22, 2023

BenchmarkDecimal/ToDecimal/uint256-8         	   12324	    102673 ns/op	   11344 B/op	     248 allocs/op
BenchmarkDecimal/ToPrettyDecimal/uint256-8   	    9595	    110235 ns/op	   14720 B/op	     251 allocs/op
BenchmarkDecimal/ToDecimal/big-8             	   10000	    133930 ns/op	   31920 B/op	     594 allocs/op

Now both of them are faster (and less alloc:y) than the big.Int decimal-print.

@holiman
Copy link
Owner Author

holiman commented Mar 22, 2023

  • fuzzing for them

@holiman holiman changed the title conversion: add PrettyDec method, more benches conversion: add PrettyDec, optimize Dec, add benchmarks and fuzzing Mar 22, 2023
@holiman holiman merged commit 01ef9cd into master Mar 22, 2023
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

Successfully merging this pull request may close these issues.

1 participant