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!newsfeed2a.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.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '"key': 0.16; '10000000': 0.16; 'indexerror:': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:bit': 0.16; 'unimportant': 0.16; 'index': 0.16; 'wrote:': 0.18; 'fit': 0.20; 'header:User-Agent:1': 0.23; 'skip:" 20': 0.27; 'header:X -Complaints-To:1': 0.27; '"",': 0.31; 'file': 0.32; 'probably': 0.32; '(most': 0.33; 'version': 0.36; 'charset:us-ascii': 0.36; 'to:addr:python-list': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'range': 0.61; 'ware': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Neil Cerutti Subject: Re: A curious bit of code... Date: Thu, 13 Feb 2014 21:01:10 +0000 (UTC) Organization: Norwich University References: <4cc09129-43ee-4205-a24c-03f92b594abc@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: jackman.norwich.edu User-Agent: slrn/0.9.9p1/mm/ao (Win32) 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1392325299 news.xs4all.nl 2885 [2001:888:2000:d::a6]:34548 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:66241 On 2014-02-13, Zachary Ware wrote: > In a fit of curiosity, I did some timings: > > 'and'ed indexing: > > C:\tmp>py -m timeit -s "key = ''" "key[0] == '<' and key[-1] == '>'" > 1000000 loops, best of 3: 0.35 usec per loop > > C:\tmp>py -m timeit -s "key = ''" > 1000000 loops, best of 3: 0.398 usec per loop > > C:\tmp>py -m timeit -s "key = 'test>'" "key[0] == '<' and key[-1] == '>'" > 1000000 loops, best of 3: 0.188 usec per loop > > C:\tmp>py -m timeit -s "key = 'test'" "key[0] == '<' and key[-1] == '>'" > 10000000 loops, best of 3: 0.211 usec per loop > > C:\tmp>py -m timeit -s "key = ''" "key[0] == '<' and key[-1] == '>'" > Traceback (most recent call last): > File "P:\Python34\lib\timeit.py", line 292, in main > x = t.timeit(number) > File "P:\Python34\lib\timeit.py", line 178, in timeit > timing = self.inner(it, self.timer) > File "", line 6, in inner > key[0] == '<' and key[-1] == '>' > IndexError: string index out of range The corrected version key and key[0] == '<' and key[-1] == '>' probably still wins the Pretty Unimportant Olympics. -- Neil Cerutti