From: Robert Griesemer Date: Wed, 23 Sep 2015 17:05:44 +0000 (-0700) Subject: math/big: factored out an internal accessor method (cleanup), added benchmark X-Git-Tag: go1.6beta1~996 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=59a6ba56347cb83c7e84e8d1eb9fbcd98ec99b59;p=gostls13.git math/big: factored out an internal accessor method (cleanup), added benchmark Current result of DecimalConversion benchmark (for future reference): BenchmarkDecimalConversion-8 10000 204770 ns/op Measured on Mac Mini (late 2012) running OS X 10.10.5, 2.3 GHz Intel Core i7, 8 GB 1333 MHz DDR3. Also: Removed comment suggesting to implement decimal by representing digits as numbers 0..9 rather than ASCII chars '0'..'9' to avoid repeated +/-'0' operations. Tried and it appears (per above benchmark) that the +/-'0' operations are neglibile but the addition conversion passes around it are not and that it makes things significantly slower. Change-Id: I6ee033b1172043248093cc5d02abff5fc54c2e7a Reviewed-on: https://go-review.googlesource.com/14857 Reviewed-by: Brad Fitzpatrick Run-TryBot: Robert Griesemer TryBot-Result: Gobot Gobot Reviewed-by: Alan Donovan --- diff --git a/src/math/big/decimal.go b/src/math/big/decimal.go index 7789677f76..b9e181dba3 100644 --- a/src/math/big/decimal.go +++ b/src/math/big/decimal.go @@ -29,6 +29,14 @@ type decimal struct { exp int // exponent } +// at returns the i'th mantissa digit, starting with the most significant digit at 0. +func (d *decimal) at(i int) byte { + if 0 <= i && i < len(d.mant) { + return d.mant[i] + } + return '0' +} + // Maximum shift amount that can be done in one pass without overflow. // A Word has _W bits and (1<> s, for s <= maxShift. func shr(x *decimal, s uint) { // Division by 1< 0 { buf = append(buf, '.') for i := 0; i < prec; i++ { - ch := byte('0') - if j := d.exp + i; 0 <= j && j < len(d.mant) { - ch = d.mant[j] - } - buf = append(buf, ch) + buf = append(buf, d.at(d.exp+i)) } }