Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed1a.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.042 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.01; 'python3': 0.07; '[0]': 0.09; 'toss': 0.09; 'def': 0.12; '10000000': 0.16; 'internally': 0.16; 'reedy': 0.16; 'skip:t 100': 0.16; 'subject:search': 0.16; 'wrote:': 0.18; 'slightly': 0.19; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'probably': 0.32; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'to:addr:python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'ian': 0.60; 'results.': 0.60; '2015': 0.84; '6.1': 0.84; 'subject:find': 0.84; 'subject:Best': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=Qptegdtl08r2/HL+bvQgbubeK68j97ka5L6eSV664Ew=; b=laR4upvxRzaZxY+DFuvRztREc/YmGO6mkg26lDpBKTpJTsbhcjSumwCspPT8aXIu89 Oasou/JgHIi5Jbbjn20a2Xhh9zKHWiWY4morCEb7m8WXDVZc37SZgw/mTkRPeOgzYwm9 Hnt4/w8LEboDC9nyEwwVthUuNO7Q/jZJVdkOn6Jc3F/EUkiHMl/gz22BH95V7BijVCra stCZMOn7ZVSWYRAtpBMKd4ERPJtJUYX6utTuXiNiWuHrEMd8t/3c7JsPJhAW7C/s2/oc p2QF15XCRv2VEOh5aPDLkvC7qwxgZ5IoGBfIylBtvoyQDm1QUPYH7/W050USwozY87Kh PO4A== X-Received: by 10.42.164.7 with SMTP id e7mr22242878icy.70.1428434421502; Tue, 07 Apr 2015 12:20:21 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <2e3a3c01-20b3-4948-9b32-bd80ed46822b@googlegroups.com> <9fd13f49-8bfa-43ef-8787-21d5c76a6268@googlegroups.com> From: Ian Kelly Date: Tue, 7 Apr 2015 13:19:41 -0600 Subject: Re: Best search algorithm to find condition within a range To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20 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: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1428434424 news.xs4all.nl 2834 [2001:888:2000:d::a6]:34356 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88611 On Tue, Apr 7, 2015 at 12:55 PM, Terry Reedy wrote: > On 4/7/2015 1:44 PM, Ian Kelly wrote: > >>>>> def to_base(number, base): >> >> ... digits = [] >> ... while number > 0: >> ... digits.append(number % base) >> ... number //= base >> ... return digits or [0] >> ... >>>>> >>>>> >>>>> to_base(2932903594368438384328325832983294832483258958495845849584958458435439543858588435856958650865490, >>>>> 429496729) >> >> [27626525, 286159541, 134919277, 305018215, 329341598, 48181777, >> 79384857, 112868646, 221068759, 70871527, 416507001, 31] >> About 15 microseconds. > > > % and probably // call divmod internally and toss one of the results. > Slightly faster (5.7 versus 6.1 microseconds on my machine) is Not on my box. $ python3 -m timeit -s "n = 1000000; x = 42" "n % x; n // x" 10000000 loops, best of 3: 0.105 usec per loop $ python3 -m timeit -s "n = 1000000; x = 42" "divmod(n,x)" 10000000 loops, best of 3: 0.124 usec per loop