-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(math): adding Dec.SdkIntTrim (#988)
* feat(math): adding Dec.SdkIntTrim * adding string benchmark * Update types/math/dec_bench_test.go Co-authored-by: Ryan Christoffersen <[email protected]> Co-authored-by: Ryan Christoffersen <[email protected]>
- Loading branch information
1 parent
21d4003
commit 404cc06
Showing
3 changed files
with
129 additions
and
22 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package math | ||
|
||
import ( | ||
"testing" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
func BenchmarkSdkIntTrim(b *testing.B) { | ||
s := "12345678901234567890.12345678901234567890" | ||
d, err := NewDecFromString(s) | ||
if err != nil { | ||
b.Error("can't convert test number") | ||
} | ||
|
||
b.Run("exp", func(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
d.SdkIntTrim() | ||
} | ||
}) | ||
|
||
b.Run("quo-integer", func(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
sdkIntTrimQuo(d) | ||
} | ||
}) | ||
|
||
b.Run("string", func(b *testing.B) { | ||
for n := 0; n < b.N; n++ { | ||
sdkIntTrimNaive(d) | ||
} | ||
}) | ||
|
||
} | ||
|
||
func sdkIntTrimQuo(d Dec) sdk.Int { | ||
d, err := d.QuoInteger(NewDecFromInt64(1)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
i, err := d.BigInt() | ||
if err != nil { | ||
panic(err) | ||
} | ||
return sdk.NewIntFromBigInt(i) | ||
} | ||
|
||
func sdkIntTrimNaive(d Dec) sdk.Int { | ||
d, err := d.QuoInteger(NewDecFromInt64(1)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
s := d.String() | ||
i, ok := sdk.NewIntFromString(s) | ||
if !ok { | ||
panic("can't convert from string") | ||
} | ||
return i | ||
} |
This file contains 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