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


Groups > comp.lang.python > #33533

Re: Python Interview Questions

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!newsfeed.datemas.de!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ian.g.kelly@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; 'operator': 0.03; 'subject:Python': 0.05; 'python': 0.09; 'incorrect': 0.09; 'lawrence': 0.09; 'throw': 0.09; 'tuple': 0.09; '2.7': 0.13; '"import': 0.16; '3.3,': 0.16; 'appends': 0.16; 'iirc': 0.16; 'iteration,': 0.16; 'wrote:': 0.17; 'figures': 0.17; 'memory': 0.18; 'bit': 0.21; 'subject:Questions': 0.22; 'tuples': 0.22; 'header:In-Reply-To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'this?': 0.28; '100000': 0.29; 'probably': 0.29; "i'm": 0.29; 'received:209.85.215.46': 0.30; 'lists': 0.31; 'idle': 0.33; 'much.': 0.33; 'anyone': 0.33; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'faster': 0.35; 'nov': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'anything': 0.36; 'item': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'skip:o 20': 0.38; 'to:addr:python.org': 0.39; 'build': 0.39; 'short': 0.39; 'header:Received:5': 0.40; 'back': 0.62; 'more': 0.63; '10000': 0.65; 'direct': 0.69; 'obvious': 0.71; '7-8%': 0.84; 'different.': 0.84; 'gap': 0.84; 'subject:Interview': 0.84; 'timings': 0.84; 'to:name:python': 0.84; '410': 0.91; 'faster.': 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=nZ9YMzMMutYPLHoCBNZc+1TnjoUs6wBm0FyxU0528PA=; b=ZOKc+FOkVqsqoFuxAy/ZyZTY82imywJYqdgP3oGxehLtvy1X+0QELF6/K5Gv/j5iOV ESxd9Zp6Ry5SuECNM5rleTTO7yJyRO5yEDC54Is44puiEcBIW37122me2lObqLosK3ZK VixRncu03leZHmeMicqu0kN3DX5ND4od7Vz9Rbu4o6lMjl+Wv/v4HbBubB8mE7IaUxX8 q5iZH8/MS0TTVW4Hug0qOjkx4Lj4PZJAXUTqvgwuB/SOUmm7Ds/FDpT5xYGokiDK3EMc ae7b7ZrXPe2nKczMTZTHIaGe0mjF82QICjOOwtNL+Ixwk1oH14uKXY65xyoimwS3j6/R Pt5w==
MIME-Version 1.0
In-Reply-To <k8c65l$hmb$1@ger.gmane.org>
References <1193768041.349129.26350@v3g2000hsg.googlegroups.com> <55443eb7-847c-4f4c-8d04-1e6b507aac00@googlegroups.com> <50a8acdc$0$29978$c3e8da3$5496439d@news.astraweb.com> <roy-EFE1F1.08532518112012@news.panix.com> <50a911ec$0$29978$c3e8da3$5496439d@news.astraweb.com> <roy-B2D5FF.12535018112012@news.panix.com> <50a97de0$0$29983$c3e8da3$5496439d@news.astraweb.com> <roy-BD53B0.21093618112012@news.panix.com> <k8c65l$hmb$1@ger.gmane.org>
From Ian Kelly <ian.g.kelly@gmail.com>
Date Sun, 18 Nov 2012 23:01:48 -0700
Subject Re: Python Interview Questions
To Python <python-list@python.org>
Content-Type text/plain; charset=ISO-8859-1
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.3814.1353304941.27098.python-list@python.org> (permalink)
Lines 103
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1353304941 news.xs4all.nl 6856 [2001:888:2000:d::a6]:56457
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:33533

Show key headers only | View raw


On Sun, Nov 18, 2012 at 7:42 PM, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> To throw a chiseldriver into the works, IIRC a tuple is way faster to create
> but accessing a list is much faster.  The obvious snag is that may have been
> Python 2.7 whereas 3.3 is completely different.  Sorry but I'm currently
> wearing my XXXL size Lazy Bone Idle Hat so have no figures to back my
> probably incorrect memory up, anyone know anything about this?

It's not been my experience with Python 2.7 that list access is faster
than tuple access.  Tuples are as fast as or faster than lists, pretty
much universally.  They seem to have closed the gap a bit in
Python 3.3, though, as the following timings show.  For one-shot
construction, tuples seem to be more efficient for short sequences,
but then lists win for longer sequences, although not by much.  Of
course, lists are always going to be much slower if you build them up
with appends and extends.

C:\>python -m timeit -s "x = range(10)" "tuple(x)"
1000000 loops, best of 3: 0.773 usec per loop

C:\>python -m timeit -s "x = range(10)" "list(x)"
1000000 loops, best of 3: 0.879 usec per loop

C:\>python -m timeit -s "x = range(100)" "tuple(x)"
100000 loops, best of 3: 2.88 usec per loop

C:\>python -m timeit -s "x = range(100)" "list(x)"
100000 loops, best of 3: 2.63 usec per loop

C:\>python -m timeit -s "x = range(1000)" "tuple(x)"
10000 loops, best of 3: 37.4 usec per loop

C:\>python -m timeit -s "x = range(1000)" "list(x)"
10000 loops, best of 3: 36.2 usec per loop

C:\>python -m timeit -s "x = range(10000)" "tuple(x)"
1000 loops, best of 3: 418 usec per loop

C:\>python -m timeit -s "x = range(10000)" "list(x)"
1000 loops, best of 3: 410 usec per loop


For iteration, tuples are consistently 7-8% faster.


C:\>python -m timeit -s "x = tuple(range(10))" "for i in x: pass"
1000000 loops, best of 3: 0.467 usec per loop

C:\>python -m timeit -s "x = list(range(10))" "for i in x: pass"
1000000 loops, best of 3: 0.498 usec per loop

C:\>python -m timeit -s "x = tuple(range(100))" "for i in x: pass"
100000 loops, best of 3: 3.31 usec per loop

C:\>python -m timeit -s "x = list(range(100))" "for i in x: pass"
100000 loops, best of 3: 3.56 usec per loop

C:\>python -m timeit -s "x = tuple(range(1000))" "for i in x: pass"
10000 loops, best of 3: 31.6 usec per loop

C:\>python -m timeit -s "x = list(range(1000))" "for i in x: pass"
10000 loops, best of 3: 34.3 usec per loop

C:\>python -m timeit -s "x = tuple(range(10000))" "for i in x: pass"
1000 loops, best of 3: 318 usec per loop

C:\>python -m timeit -s "x = list(range(10000))" "for i in x: pass"
1000 loops, best of 3: 341 usec per loop


For direct item access, tuples seem to be about 2-3% faster.


C:\>python -m timeit -s "import operator as o; x = tuple(range(10)); g
= o.itemgetter(*range(len(x)))" "g(x)"
1000000 loops, best of 3: 0.67 usec per loop

C:\>python -m timeit -s "import operator as o; x = list(range(10)); g
= o.itemgetter(*range(len(x)))" "g(x)"
1000000 loops, best of 3: 0.674 usec per loop

C:\>python -m timeit -s "import operator as o; x = tuple(range(100));
g = o.itemgetter(*range(len(x)))" "g(x)"
100000 loops, best of 3: 4.52 usec per loop

C:\>python -m timeit -s "import operator as o; x = list(range(100)); g
= o.itemgetter(*range(len(x)))" "g(x)"
100000 loops, best of 3: 4.65 usec per loop

C:\>python -m timeit -s "import operator as o; x = tuple(range(1000));
g = o.itemgetter(*range(len(x)))" "g(x)"
10000 loops, best of 3: 43.2 usec per loop

C:\>python -m timeit -s "import operator as o; x = list(range(1000));
g = o.itemgetter(*range(len(x)))" "g(x)"
10000 loops, best of 3: 43.7 usec per loop

C:\>python -m timeit -s "import operator as o; x =
tuple(range(10000)); g = o.itemgetter(*range(len(x)))" "g(x)"
1000 loops, best of 3: 422 usec per loop

C:\>python -m timeit -s "import operator as o; x = list(range(10000));
g = o.itemgetter(*range(len(x)))" "g(x)"
1000 loops, best of 3: 447 usec per loop

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Re: Python Interview Questions chinjannisha@gmail.com - 2012-11-17 10:01 -0800
  Re: Python Interview Questions Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-11-18 01:54 -0500
  Re: Python Interview Questions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-18 09:39 +0000
    Re: Python Interview Questions Roy Smith <roy@panix.com> - 2012-11-18 08:53 -0500
      Re: Python Interview Questions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-18 16:50 +0000
        Re: Python Interview Questions "D'Arcy J.M. Cain" <darcy@druid.net> - 2012-11-18 12:16 -0500
        Re: Python Interview Questions Roy Smith <roy@panix.com> - 2012-11-18 12:53 -0500
          Re: Python Interview Questions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-19 00:31 +0000
            Re: Python Interview Questions Roy Smith <roy@panix.com> - 2012-11-18 21:09 -0500
              Re: Python Interview Questions Chris Angelico <rosuav@gmail.com> - 2012-11-19 13:18 +1100
              Re: Python Interview Questions Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-19 02:42 +0000
              Re: Python Interview Questions Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-18 23:01 -0700
              Re: Python Interview Questions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-19 07:54 +0000
                Re: Python Interview Questions Roy Smith <roy@panix.com> - 2012-11-19 09:30 -0500
                Re: Python Interview Questions Ian Kelly <ian.g.kelly@gmail.com> - 2012-11-19 09:44 -0700
                Re: Python Interview Questions Terry Reedy <tjreedy@udel.edu> - 2012-11-19 15:41 -0500
                Re: Python Interview Questions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-19 23:42 +0000
                Re: Python Interview Questions Roy Smith <roy@panix.com> - 2012-11-19 21:33 -0500
                Re: Python Interview Questions Roy Smith <roy@panix.com> - 2012-11-19 09:59 -0500
                Re: Python Interview Questions Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-19 23:53 +0000
                Re: Python Interview Questions Roy Smith <roy@panix.com> - 2012-11-19 22:14 -0500
                RE: Python Interview Questions "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-11-19 23:57 +0000
              Re: Python Interview Questions Terry Reedy <tjreedy@udel.edu> - 2012-11-19 03:27 -0500
        Re: Python Interview Questions Chris Angelico <rosuav@gmail.com> - 2012-11-19 07:02 +1100

csiph-web