Skip to content

Instantly share code, notes, and snippets.

@svartalf
Created May 27, 2012 13:11
Show Gist options
  • Save svartalf/2814163 to your computer and use it in GitHub Desktop.
Save svartalf/2814163 to your computer and use it in GitHub Desktop.
Test of the list and tuple __getitem__ performance
from __future__ import print_function
import sys
import timeit
l = list(range(10))
t = tuple(l)
def func(obj):
r = list(range(1000))
for i in r:
for j in r:
c = obj[2]
print(sys.version)
print(timeit.Timer('func(l)', 'from __main__ import l, func').timeit(number=100))
print(timeit.Timer('func(t)', 'from __main__ import t, func').timeit(number=100))
@svartalf
Copy link
Author

Performance under a different versions of the Python is a very odd :|

[svartalf@tardis tmp]$ uname -a
Linux tardis 3.3.4-2-ARCH #1 SMP PREEMPT Wed May 2 15:39:58 UTC 2012 i686 Genuine Intel(R) CPU U2300 @ 1.20GHz GenuineIntel GNU/Linux

[svartalf@tardis tmp]$ python2.7 timing.py 
2.7.3 (default, Apr 24 2012, 00:06:13) 
[GCC 4.7.0 20120414 (prerelease)]
10.243740797
16.0057370663

[svartalf@tardis tmp]$ python3.2 timing.py 
3.2.3 (default, Apr 23 2012, 23:35:30) 
[GCC 4.7.0 20120414 (prerelease)]
21.861752033233643
21.378299951553345

[svartalf@tardis tmp]$ pypy timing.py 
2.7.2 (2346207d99463f299f09f3e151c9d5fa9158f71b, Feb 10 2012, 15:06:33)
[PyPy 1.8.0 with GCC 4.6.2]
0.593545913696
1.0119998455

@svartalf
Copy link
Author

Additional test under a different system

[svartalf@svartalf-work tmp]$ uname -a
Linux svartalf-work 3.3.6-1-ARCH #1 SMP PREEMPT Sun May 13 10:52:32 CEST 2012 x86_64 GNU/Linux

[svartalf@svartalf-work tmp]$ python2.6 timing.py 
2.6.7 (r267:88850, Nov 11 2011, 16:29:26) 
[GCC 4.6.1 20110819 (prerelease)]
4.16057491302
5.19009113312

[svartalf@svartalf-work tmp]$ python2.7 timing.py 
2.7.3 (default, Apr 24 2012, 00:00:54) 
[GCC 4.7.0 20120414 (prerelease)]
4.03914189339
5.13983201981

[svartalf@svartalf-work tmp]$ python3.2 timing.py 
3.2.3 (default, Apr 23 2012, 23:14:44) 
[GCC 4.7.0 20120414 (prerelease)]
4.565101861953735
4.454107999801636

[svartalf@svartalf-work tmp]$ pypy timing.py 
2.7.2 (2346207d99463f299f09f3e151c9d5fa9158f71b, Feb 10 2012, 15:07:02)
[PyPy 1.8.0 with GCC 4.6.2]
0.144304037094
0.292443990707

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment