Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #52952

Re: Fast conversion of numbers to numerator/denominator pairs

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 <timothy.c.delaney@gmail.com>
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 <timothy.c.delaney@gmail.com>
To "Steven D'Aprano" <steve+comp.lang.python@pearwood.info>
Content-Type multipart/alternative; boundary=047d7b5d33d452421404e4b8a292
Cc Python-List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.202.1377381558.19984.python-list@python.org> (permalink)
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

Show key headers only | View raw


[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 | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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