Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'preferred.': 0.05; 'exp': 0.09; 'function:': 0.09; 'method,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'tmp': 0.09; 'python': 0.11; 'def': 0.12; '(1,': 0.16; '(2,': 0.16; '*must*': 0.16; 'enough.': 0.16; 'form"': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'sign,': 0.16; 'simplest': 0.16; 'wrote:': 0.18; 'things.': 0.19; 'select': 0.22; 'header:User-Agent:1': 0.23; "i've": 0.25; 'subject:/': 0.26; 'code:': 0.26; 'header:X-Complaints-To:1': 0.27; 'tried': 0.27; "doesn't": 0.30; 'code': 0.31; "d'aprano": 0.31; 'decimal': 0.31; 'faster,': 0.31; 'steven': 0.31; 'subject:numbers': 0.31; 'lists': 0.32; 'cases': 0.33; 'skip:b 30': 0.33; 'maybe': 0.34; 'could': 0.34; 'convert': 0.35; 'form.': 0.35; 'but': 0.35; 'there': 0.35; 'example,': 0.37; 'list': 0.37; 'being': 0.38; 'ends': 0.38; 'to:addr:python-list': 0.38; 'little': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'easy': 0.60; 'conversion': 0.61; 'numbers': 0.61; 'course': 0.61; 'first': 0.61; 'times': 0.62; 'skip:n 10': 0.64; 'fifty': 0.84; 'subject:Fast': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Fast conversion of numbers to numerator/denominator pairs Date: Sat, 24 Aug 2013 14:52:59 +0200 Organization: None References: <521828e7$0$29986$c3e8da3$5496439d@news.astraweb.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p508494a9.dip0.t-ipconnect.de User-Agent: KNode/4.7.3 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: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1377348775 news.xs4all.nl 15899 [2001:888:2000:d::a6]:47271 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:52938 Steven D'Aprano wrote: > I have a need to convert arbitrary non-complex numbers into numerator/ > denominator pairs. Numbers could be ints, floats, Fractions or Decimals. > For example: > > 2 => (2, 1) > 0.25 => (1, 4) > Fraction(2, 3) => (2, 3) > Decimal("0.5") => (1, 2) > > > The first three cases are easy and fast: > > # ints and Fractions > number.numerator, number.denominator > > # floats are a little slower > number.as_integer_ratio() > > > But Decimals are unfortunately slower. MUCH slower, about 40 times slower > than Fractions in Python 3.3: > > tmp = Fraction.from_decimal(number) > (tmp.numerator, tmp.denominator) > > > This ends up being the bottleneck in my code: once you include the > scaffolding code to select the right conversion method, processing a > large list of Decimals is about fifty times slower than large lists of > floats or fractions. > > Is there a fast way to convert a Decimal into a pair of numbers numerator/ > denominator? It *must* be exact, but it doesn't have to be simplest form. > For example, Decimal("0.5") => (5, 10) would be okay, although (1, 2) > would be preferred. > > > I've tried this function: > > 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? Maybe these micro-optimisations will be sufficient: _trans = bytes.maketrans(bytes(range(10)), b"0123456789") def convert(d): sign, digits, exp = d.as_tuple() num = int(bytes(digits).translate(_trans)) if sign: num = -num return num, 10**-exp You can get the "simplest form" with co-prime numerator and denominator by dividing by fractions.gcd(), but that will of course slow down things.