Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #52952
| References | <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> |
|---|---|
| Date | 2013-08-25 07:59 +1000 |
| Subject | Re: Fast conversion of numbers to numerator/denominator pairs |
| From | Tim Delaney <timothy.c.delaney@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.202.1377381558.19984.python-list@python.org> (permalink) |
[Multipart message — attachments visible in raw view] - view raw
On 24 August 2013 13:30, Steven D'Aprano <
steve+comp.lang.python@pearwood.info> wrote:
>
> def convert(d):
> sign, digits, exp = d.as_tuple()
> num = int(''.join([str(digit) for digit in digits]))
> if sign: num = -num
> return num, 10**-exp
>
> which is faster, but not fast enough. Any suggestions?
>
Straightforward multiply and add takes about 60% of the time for a single
digit on my machine compared to the above, and 55% for 19 digits (so
reasonably consistent). It's about 10x slower than fractions.
def convert_muladd(d, _trans=_trans, bytes=bytes):
sign, digits, exp = d.as_tuple()
num = 0
for digit in digits:
num *= 10
num += digit
if sign:
num = -num
return num, 10**-exp
Breakdown of the above (for 19 digits):
d.as_tuple() takes about 35% of the time.
The multiply and add takes about 55% of the time.
The exponentiation takes about 10% of the time.
Tim Delaney
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Fast conversion of numbers to numerator/denominator pairs Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-08-24 03:30 +0000 Re: Fast conversion of numbers to numerator/denominator pairs Ian Kelly <ian.g.kelly@gmail.com> - 2013-08-24 01:37 -0600 Re: Fast conversion of numbers to numerator/denominator pairs Ian Kelly <ian.g.kelly@gmail.com> - 2013-08-24 01:50 -0600 Re: Fast conversion of numbers to numerator/denominator pairs Peter Otten <__peter__@web.de> - 2013-08-24 14:52 +0200 Re: Fast conversion of numbers to numerator/denominator pairs Tim Delaney <timothy.c.delaney@gmail.com> - 2013-08-25 07:59 +1000 Re: Fast conversion of numbers to numerator/denominator pairs Tim Delaney <timothy.c.delaney@gmail.com> - 2013-08-25 08:05 +1000
csiph-web