uint256 icon indicating copy to clipboard operation
uint256 copied to clipboard

uint256: optimize leadingZeros and add BenchmarkBitLen

Open AaronChen0 opened this issue 1 year ago • 1 comments

go test ./...

returns

ok  	github.com/holiman/uint256	0.977s

Bechmark for leadingZeros

go test -run - -bench BenchmarkLead -benchmem

code:

func BenchmarkLeadingZeros(bench *testing.B) {
	// res is a variable to prevent the golang compiler from eliminating the benchmark testing entirely.
	var res int

	data := []Int{
		{0, 0, 0, 0xffffffffffffffff},
		{0, 0, 0xffffffffffffffff, 0},
		{0, 0xffffffffffffffff, 0, 0},
		{0xffffffffffffffff, 0, 0, 0},
	}

	benchmarkLeadingZeros := func(b *testing.B, num *Int) {
		var r int
		for i := 0; i < b.N; i++ {
			r = leadingZeros(num)
		}
		res = r
	}

	res += 1

	bench.Run("leadingZeros192/uint256", func(b *testing.B) { benchmarkLeadingZeros(b, &data[0])})
	bench.Run("leadingZeros128/uint256", func(b *testing.B) { benchmarkLeadingZeros(b, &data[1])})
	bench.Run("leadingZeros64/uint256", func(b *testing.B) { benchmarkLeadingZeros(b, &data[2])})
	bench.Run("leadingZeros0/uint256", func(b *testing.B) { benchmarkLeadingZeros(b, &data[3])})
}

old:

goos: linux
goarch: amd64
pkg: github.com/holiman/uint256
cpu: AMD Ryzen 7 7735H with Radeon Graphics         
BenchmarkLeadingZeros/leadingZeros192/uint256-16         	238305988	         4.919 ns/op	       0 B/op	       0 allocs/op
BenchmarkLeadingZeros/leadingZeros128/uint256-16         	232537900	         4.886 ns/op	       0 B/op	       0 allocs/op
BenchmarkLeadingZeros/leadingZeros64/uint256-16          	244867848	         5.017 ns/op	       0 B/op	       0 allocs/op
BenchmarkLeadingZeros/leadingZeros0/uint256-16           	244553245	         5.197 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/holiman/uint256	6.845s

new:

goos: linux
goarch: amd64
pkg: github.com/holiman/uint256
cpu: AMD Ryzen 7 7735H with Radeon Graphics         
BenchmarkLeadingZeros/leadingZeros192/uint256-16         	1000000000	         1.098 ns/op	       0 B/op	       0 allocs/op
BenchmarkLeadingZeros/leadingZeros128/uint256-16         	906194443	         1.316 ns/op	       0 B/op	       0 allocs/op
BenchmarkLeadingZeros/leadingZeros64/uint256-16          	779154236	         1.565 ns/op	       0 B/op	       0 allocs/op
BenchmarkLeadingZeros/leadingZeros0/uint256-16           	662557696	         1.786 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/holiman/uint256	5.301s

AaronChen0 avatar Apr 16 '24 11:04 AaronChen0

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 100.00%. Comparing base (e290e7a) to head (97e0084). Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff            @@
##            master      #156   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            5         5           
  Lines         1649      1642    -7     
=========================================
- Hits          1649      1642    -7     

codecov[bot] avatar Apr 16 '24 11:04 codecov[bot]

Hi. Can someone give some feedbacks on my recent PRs?

AaronChen0 avatar May 04 '24 12:05 AaronChen0

Yep, I will look into it the coming week. Thanks for the PRs!

holiman avatar May 04 '24 12:05 holiman