Forum Archive

Relative performance of Python2 vs. Python3

ccc

https://github.com/cclauss/fractal_hacks/blob/master/cc_mandelbrot.py

On my MacBook Pro takes ~14 sec in Python2 and ~17 sec in Python3.

On my iPad takes ~30 sec in Python2 and ~75 sec in Python3.

Execution time greater than 2x seems a bit high. Thoughts?

lukaskollmer

@ccc The MacBook has a far more advanced CPU, it can take better advantage of multithreading and the optimizations. Also: much more ram

ccc

My point was not about not about the hardware difference (I was a Systems Engineer at Apple for 7 years -- I know about hardware) my point was that P2 and P3 on the Mac perform almost identically but in Pythonista there is a huge disparity...

lukaskollmer

@ccc Ok sorry, I thought you were talking about the hardware.

dgelessus

A big part of the performance difference is probably because Python 3 uses "long" (arbitrary size) integers for the int type everywhere. In Python 2 there were two integer types, int and long. int was limited to the size of a C int and automatically overflowed into long, which was arbitrary size. So for anything that fits into a C int there is a significant speed difference between Python 2 and 3.

On my iPad mini it takes about 11 seconds under Python 2 and 19 seconds under Python 3 to generate a Mandelbrot image (size 4) with your script. If I convert some of the integer constants to longs under Python 2, the time goes up to 15 seconds.

Phuket2

Just for comparison ,
IPad Pro 12.9'
Py3
Please wait: calculating Mandelbrot set...
IMG_SIZE=16 (1760, 640) elapsed time: 0:00:24.850443

Py3 , but run as 2.7
Please wait: calculating Mandelbrot set...
IMG_SIZE=16 (1760, 640) elapsed time: 0:00:15.299718

dgelessus

@Phuket2 Wow, you're lucky. On my iPad mini, when I run the script with size 16 on Python 3, it takes 5 minutes to run...

Phuket2

@dgelessus , that's an amazing difference. I have been so impressed with my iPad Pro, screen score, speed and battery performance. The battery is the same performance as my iPad Air 2. Britney's settings always the same both devices. I get 10/11 good hours out of my battery. Only difference between the 2 is that the Pro takes a lot longer to charge than the air.
Here is an interesting list if you are interested. Geek bench test comparison for iOS devices
http://browser.primatelabs.com/ios-benchmarks

ccc

@Phuket2 I am surprised if Python 3 ran faster for you than Python 2.

Could you please redownload the code and running it again? I modified it to print the version of Python that is running. If you are in Pythonista 3 beta then you need to modify the first line of the script to read python2 instead of python3 to force Pythonista to use of Python 2.

omz

I'm seeing similar results, and unfortunately, I have neither a good explanation, nor a solution at the moment.

I've experimented a little bit with enabling additional compiler optimizations for the Python 3 branch. That does result in a ~20% speedup (from 1:10 to 0:59), but it's still significantly slower than running it with Python 2 (0:35). Interestingly, changing the integer literals to explicit longs in Python 2 brings it down to 0:50, so very close to Python 3, where all integers are longs, but that doesn't really explain why there isn't much of a difference on the desktop...

abcabc

Implementing this in "fragment shader" would run in less than a fraction of a second. See
the following gist for an implementation.
https://gist.github.com/balachandrana/82f643e38326ce9e7ce720e399b34cae

It may not be directly related to python 2/python 3 performance comparison topic. But
"fragment shaders" are well suited for implementing these kind of problems. For learning about
"fragment shaders" the book of Shaders (http://patriciogonzalezvivo.com/2015/thebookofshaders/) is
the best place to start with. The following tutorials from shadertoy are also good for beginners.
1. https://www.shadertoy.com/view/Md23DV
2. https://www.shadertoy.com/results?query=lesson

omz

I've made some progress. It turns out that Python 3 was compiled with pymalloc disabled.

Enabling it (as in the Python 2.7 build) results in a pretty significant speedup. Now I'm getting 0:41 from Python 3, which is quite a bit closer to Python 2 (0:35), and the ratio is very similar to what @ccc is seeing on the MacBook Pro.

ccc

Awesome work both of you!!!

@abcabc I posted some suggestions as a comment on the gist on how to be a bit more full screen.

Is there a similar way to use to the hardware of a Mac from Python?!? Your speedup is incredible!

@omz I think your speedup is great news for us all! We all want to move to Python 3 ;-) Right?!?

Webmaster4o

@ccc I'm certainly eager to move to Py3. I'm trying to do it all without 2to3, as well as to keep backwards compatibility, because it's a good way to learn imo

omz

@ccc @abcabc The shader is great! I've made a resolution-independent fork that renders full-screen on all devices. The hard-coded 600x600 size probably doesn't work well on iPhone.

abcabc

@omz, @ccc Thanks for your suggestions.

Is there a similar way to use to the hardware of a Mac from Python?!?

Pyglet is supposed to be good for opengl. There seems to be simple wrapper for
GLSL (see the url below), but I have not tried it.
https://groups.google.com/forum/#!topic/pyglet-users/3x8rYF7pNC4