Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.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.029 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.01; 'python3': 0.07; 'lookup': 0.09; 'toss': 0.09; 'cc:addr:python-list': 0.11; '10000000': 0.16; '42;': 0.16; 'eliminating': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'internally': 0.16; 'subject:search': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'slightly': 0.19; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In- Reply-To:1': 0.27; 'am,': 0.29; 'message-id:@mail.gmail.com': 0.30; 'time:': 0.31; 'probably': 0.32; 'but': 0.35; 'received:google.com': 0.35; 'ian': 0.60; 'results.': 0.60; '2015': 0.84; '6.1': 0.84; 'cuts': 0.84; 'subject:find': 0.84; 'subject:Best': 0.91; 'to:none': 0.92 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:cc :content-type; bh=sWaOeh3k22iuIXcGEgy9GoOwzFKILkJDGlLZV4NFNrI=; b=DT7/hxKrrxPRVk31SREn8fYXr6ILLtAVEb779ty0OGmgxR08sqe+cHzenB4kd2INag wny+EPZYb8WjER7jS6MXc/Lk2VSzxmbOkU6gfod3weFthAXwcaZoxYIKe8AJSLGTk9DH MxpTQYm/CxQTCLrEN3iR6RzG6FPAy48oS7HvaNu93/0v+GcL7M/bc/o/DceUtAex2juN 9fiqMV1LvUlfe20Ei59i65Jrjx5+RLLQ4X6uG1XgCDw5yQk6Dcyh8Oxs0XpTsehTgS2e 9tWGV8auzJnrIkgcFH4Bd0UgNy0P4j9zrsWuUHkqcvtGCAtIJi9rhM0NJ7VVikqor6dw 1QAQ== MIME-Version: 1.0 X-Received: by 10.107.160.212 with SMTP id j203mr33229664ioe.43.1428434735843; Tue, 07 Apr 2015 12:25:35 -0700 (PDT) In-Reply-To: References: <2e3a3c01-20b3-4948-9b32-bd80ed46822b@googlegroups.com> <9fd13f49-8bfa-43ef-8787-21d5c76a6268@googlegroups.com> Date: Wed, 8 Apr 2015 05:25:35 +1000 Subject: Re: Best search algorithm to find condition within a range From: Chris Angelico Cc: 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: 24 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1428434738 news.xs4all.nl 2947 [2001:888:2000:d::a6]:38320 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:88612 On Wed, Apr 8, 2015 at 5:19 AM, Ian Kelly wrote: >> % 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 Nor mine, though eliminating the global lookup cuts some of the time: rosuav@sikorsky:~$ python3 -m timeit -s "n = 1000000; x = 42" "n % x; n // x" 1000000 loops, best of 3: 0.231 usec per loop rosuav@sikorsky:~$ python3 -m timeit -s "n = 1000000; x = 42" "divmod(n,x)" 1000000 loops, best of 3: 0.305 usec per loop rosuav@sikorsky:~$ python3 -m timeit -s "n = 1000000; x = 42; dm = divmod" "dm(n,x)" 1000000 loops, best of 3: 0.26 usec per loop But this is microbenchmarking. It's pretty pointless. ChrisA