Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33535
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Python Interview Questions |
| Date | 2012-11-19 03:27 -0500 |
| References | (5 earlier) <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> <CALwzidnr-6pA9_MRZFixOvT--Yaa9UBU+9+7F0V9o+20Pg=gpg@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.0.1353313685.29569.python-list@python.org> (permalink) |
On 11/19/2012 1:01 AM, Ian Kelly wrote: > 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. Interesting results. But what system (hardware, os). These sorts of times tend to vary with the system. > > 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 > -- Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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