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

use to determine whether it equals 0 or not. #299

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cdpzyafk
Copy link

@cdpzyafk cdpzyafk commented Nov 5, 2022

r.Cmp(zeroInt) != 0 => r.Sign() != 0

func BenchmarkCmpZeroInt(b *testing.B) {
positiveVal := big.NewInt(314)
negtiveVal := big.NewInt(-314)
b.Run("cmpzero", func(b *testing.B) {
for i := 0; i < b.N; i++ {
zeroInt.Cmp(zeroInt)
}
})

b.Run("cmppositive", func(b *testing.B) {
	for i := 0; i < b.N; i++ {
		positiveVal.Cmp(zeroInt)
	}
})

b.Run("cmpnegtive", func(b *testing.B) {
	for i := 0; i < b.N; i++ {
		negtiveVal.Cmp(zeroInt)
	}
})

b.Run("signzero", func(b *testing.B) {
	for i := 0; i < b.N; i++ {
		zeroInt.Sign()
	}
})

b.Run("signpositive", func(b *testing.B) {
	for i := 0; i < b.N; i++ {
		positiveVal.Sign()
	}
})

b.Run("signnegtive", func(b *testing.B) {
	for i := 0; i < b.N; i++ {
		negtiveVal.Sign()
	}
})

}

goos: darwin
goarch: amd64
pkg: github.com/shopspring/decimal
cpu: Intel(R) Core(TM) i5-1038NG7 CPU @ 2.00GHz
BenchmarkCmpZeroInt
BenchmarkCmpZeroInt/cmpzero
BenchmarkCmpZeroInt/cmpzero-8 729437467 1.714 ns/op 0 B/op 0 allocs/op
BenchmarkCmpZeroInt/cmppositive
BenchmarkCmpZeroInt/cmppositive-8 240192255 5.657 ns/op 0 B/op 0 allocs/op
BenchmarkCmpZeroInt/cmpnegtive
BenchmarkCmpZeroInt/cmpnegtive-8 463588371 2.386 ns/op 0 B/op 0 allocs/op
BenchmarkCmpZeroInt/signzero
BenchmarkCmpZeroInt/signzero-8 1000000000 0.4847 ns/op 0 B/op 0 allocs/op
BenchmarkCmpZeroInt/signpositive
BenchmarkCmpZeroInt/signpositive-8 1000000000 0.5291 ns/op 0 B/op 0 allocs/op
BenchmarkCmpZeroInt/signnegtive
BenchmarkCmpZeroInt/signnegtive-8 1000000000 0.5308 ns/op 0 B/op 0 allocs/op

r.Cmp(zeroInt) != 0 => r.Sign() != 0
Copy link
Member

@mwoss mwoss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I correctly understand your PR, bigInt.Sign() == 0 is way more performant than bigInt.Cmp(zeroInt) when validating if given big.Int is a zero value. Could you just reformat your PR description and title so it's a bit more descriptive, just for future reference.

@mwoss
Copy link
Member

mwoss commented Jan 10, 2024

Also deeply apologize for late review! :<

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.

2 participants