Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.025 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; '(so': 0.07; 'exp': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'enough.': 0.16; 'reasonably': 0.16; 'sign,': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.18; 'machine': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'subject:/': 0.26; '(for': 0.26; 'header:In- Reply-To:1': 0.27; 'tim': 0.29; 'compared': 0.30; 'message- id:@mail.gmail.com': 0.30; "d'aprano": 0.31; 'faster,': 0.31; 'steven': 0.31; 'subject:numbers': 0.31; 'skip:_ 10': 0.34; 'but': 0.35; 'received:google.com': 0.35; 'add': 0.35; 'above,': 0.60; 'august': 0.61; 'breakdown': 0.68; '10%': 0.74; '35%': 0.84; 'subject:Fast': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=VelNihNuBF9Mmz2LmstVr+lt/zPld6xWiasHo9palrk=; b=KykX/MtdAE2pYlMx4oFXDNwITcBmmz/p2hQwb0+O/FkGk3sGPnfzeLplCxuCGawblC InatjntLuWIQT7LLtQJph1GpKz1BHBmacKS0D87uX2UZLcxuruVwkna91eoOAt0uAzNM xiiay2hsOsCJeNhZBr5fBloknbGFVNJ6n2yQt/lGHSJAzYhfqVdOW26dj36lgmSnhGlW W5wktVi/DrZEHipjd8/0XfarqaE1f4PBSjIDMstD6UDSPzqVbFGw5G9ttBfJuUgKZ42v IjE+YY7qKreRqxMuPsYflhXL9oK7uugNOzT1L27wtNNW3hkWz+NudWy0SnodaXWAByFD kl+Q== MIME-Version: 1.0 X-Received: by 10.60.124.14 with SMTP id me14mr6336605oeb.4.1377381555255; Sat, 24 Aug 2013 14:59:15 -0700 (PDT) In-Reply-To: <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> References: <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> Date: Sun, 25 Aug 2013 07:59:15 +1000 Subject: Re: Fast conversion of numbers to numerator/denominator pairs From: Tim Delaney To: "Steven D'Aprano" Content-Type: multipart/alternative; boundary=047d7b5d33d452421404e4b8a292 Cc: Python-List X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 79 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1377381558 news.xs4all.nl 15890 [2001:888:2000:d::a6]:37030 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52952 --047d7b5d33d452421404e4b8a292 Content-Type: text/plain; charset=UTF-8 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 --047d7b5d33d452421404e4b8a292 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On 24 August 2013 13:30, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:

def convert(d):
=C2=A0 =C2=A0 sign, digits, exp =3D d.as_tuple()
=C2=A0 =C2=A0 num =3D int(''.join([str(digit) for digit in digits])= )
=C2=A0 =C2=A0 if sign: num =3D -num
=C2=A0 =C2=A0 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 1= 9 digits (so reasonably consistent). It's about 10x slower than fractio= ns.

def convert_muladd(d, _trans=3D_trans, bytes=3Dbyt= es):
=C2=A0 =C2=A0 sign, digits, exp =3D d.as_tuple()
= =C2=A0 =C2=A0 num =3D 0

=C2=A0 =C2=A0 for digit in= digits:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 num *=3D 10
=C2=A0 =C2=A0 =C2=A0 =C2=A0 num +=3D digit

= =C2=A0 =C2=A0 if sign:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 num =3D -num

=C2=A0 =C2=A0 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
--047d7b5d33d452421404e4b8a292--