Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List indexing efficiency (python 2 vs python 3)

In answering another question, I suggested to use timeit to test the difference between indexing a list with positive integers vs. negative integers. Here's the code:

import timeit t=timeit.timeit('mylist[99]',setup='mylist=list(range(100))',number=10000000) print (t) t=timeit.timeit('mylist[-1]',setup='mylist=list(range(100))',number=10000000) print (t) 

I ran this code with python 2.6:

$ python2.6 test.py 0.587687015533 0.586369991302 

Then I ran it with python 3.2:

$ python3.2 test.py 0.9212150573730469 1.0225799083709717 

Then I scratched my head, did a little google searching and decided to post these observations here.

Operating system: OS-X (10.5.8) -- Intel Core2Duo

That seems like a pretty significant difference to me (a factor of over 1.5 difference). Does anyone have an idea why python3 is so much slower -- especially for such a common operation?

EDIT

I've run the same code on my Ubuntu Linux desktop (Intel i7) and achieved comparable results with python2.6 and python 3.2. It seems that this is an issue which is operating system (or processor) dependent (Other users are seeing the same behavior on Linux machines -- See comments).

EDIT 2

The startup banner was requested in one of the answers, so here goes:

Python 2.6.4 (r264:75821M, Oct 27 2009, 19:48:32)  [GCC 4.0.1 (Apple Inc. build 5493)] on darwin 

and:

Python 3.2 (r32:88452, Feb 20 2011, 10:19:59)  [GCC 4.0.1 (Apple Inc. build 5493)] on darwin 

UPDATE

I've just installed fresh versions of python2.7.3 and python3.2.3 from http://www.python.org/download/

In both cases, I took the

"Python x.x.3 Mac OS X 32-bit i386/PPC Installer (for Mac OS X 10.3 through 10.6 [2])"

since I am on OS X 10.5. Here are the new timings (which are reasonably consistent through multiple trials):

python 2.7

$python2.7 test.py 0.577006101608 0.590042829514 

python 3.2.3

$python3.2 test.py 0.8882801532745361 1.034242868423462 
like image 799
mgilson Avatar asked Jul 09 '12 17:07

mgilson


People also ask

Which is faster Python 2 or 3?

Python 3.3 comes faster than Python 2.7.

What is difference between Python 2 and Python 3?

Python 3 is more in-demand and includes a typing system. Python 2 is outdated and uses an older syntax for the print function. While Python 2 is still in use for configuration management in DevOps, Python 3 is the current standard. Python (the code, not the snake) is a popular coding language to learn for beginners.

Is print a function in Python 2?

Print: In Python 2, “print” is treated as a statement rather than a function. There is no need to wrap the text you want to print in parentheses, although you can if you want.


2 Answers

This appears to be an artifact of some builds of Python 3.2. The best hypothesis at this point is that all 32-bit Intel builds have the slowdown, but no 64-bit ones do. Read on for further details.

You didn't run nearly enough tests to determine anything. Repeating your test a bunch of times, I got values ranging from 0.31 to 0.54 for the same test, which is a huge variation.

So, I ran your test with 10x the number, and repeat=10, using a bunch of different Python2 and Python3 installs. Throwing away the top and bottom results, averaging the other 8, and dividing by 10 (to get a number equivalent to your tests), here's what I saw:

 1. 0.52/0.53 Lion 2.6  2. 0.49/0.50 Lion 2.7  3. 0.48/0.48 MacPorts 2.7  4. 0.39/0.49 MacPorts 3.2  5. 0.39/0.48 HomeBrew 3.2 

So, it looks like 3.2 is actually slightly faster with [99], and about the same speed with [-1].

However, on a 10.5 machine, I got these results:

 1. 0.98/1.02 MacPorts 2.6  2. 1.47/1.59 MacPorts 3.2 

Back on the original (Lion) machine, I ran in 32-bit mode, and got this:

 1. 0.50/0.48 Homebrew 2.7  2. 0.75/0.82 Homebrew 3.2 

So, it seems like 32-bitness is what matters, and not Leopard vs. Lion, gcc 4.0 vs. gcc 4.2 or clang, hardware differences, etc. It would help to test 64-bit builds under Leopard, with different compilers, etc., but unfortunately my Leopard box is a first-gen Intel Mini (with a 32-bit Core Solo CPU), so I can't do that test.

As further circumstantial evidence, I ran a whole slew of other quick tests on the Lion box, and it looks like 32-bit 3.2 is ~50% slower than 2.x, while 64-bit 3.2 is maybe a little faster than 2.x. But if we really want to back that up, someone needs to pick and run a real benchmark suite.

Anyway, my best guess at this point is that when optimizing the 3.x branch, nobody put much effort into 32-bit i386 Mac builds. Which is actually a reasonable choice for them to have made.

Or, alternatively, they didn't even put much effort into 32-bit i386 period. That possibility might explain why the OP saw 2.x and 3.2 giving similar results on a linux box, while Otto Allmendinger saw 3.2 being similarly slower to 2.6 on a linux box. But since neither of them mentioned whether they were running 32-bit or 64-bit linux, it's hard to know whether that's relevant.

There are still lots of other different possibilities that we haven't ruled out, but this seems like the best one.

like image 171
abarnert Avatar answered Oct 03 '22 15:10

abarnert


here is a code that illustrates at least part of the answer:

$ python Python 2.7.3 (default, Apr 20 2012, 22:44:07)  [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import timeit >>> t=timeit.timeit('mylist[99]',setup='mylist=list(range(100))',number=50000000) >>> print (t) 2.55517697334 >>> t=timeit.timeit('mylist[99L]',setup='mylist=list(range(100))',number=50000000) >>> print (t) 3.89904499054  $ python3 Python 3.2.3 (default, May  3 2012, 15:54:42)  [GCC 4.6.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import timeit >>> t=timeit.timeit('mylist[99]',setup='mylist=list(range(100))',number=50000000) >>> print (t) 3.9906489849090576 

python3 does not have old int type.

like image 38
subdir Avatar answered Oct 03 '22 13:10

subdir