omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Relative performance of Python2 vs. Python3

    Pythonista
    7
    16
    12458
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • dgelessus
      dgelessus last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • Phuket2
        Phuket2 last edited by

        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

        1 Reply Last reply Reply Quote 0
        • dgelessus
          dgelessus last edited by

          @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 1 Reply Last reply Reply Quote 2
          • Phuket2
            Phuket2 @dgelessus last edited by

            @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

            1 Reply Last reply Reply Quote 0
            • ccc
              ccc last edited by 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.

              1 Reply Last reply Reply Quote 0
              • omz
                omz last edited by

                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...

                1 Reply Last reply Reply Quote 0
                • abcabc
                  abcabc last edited by

                  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
                  1 Reply Last reply Reply Quote 1
                  • omz
                    omz last edited by

                    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.

                    1 Reply Last reply Reply Quote 3
                    • ccc
                      ccc last edited by 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 1 Reply Last reply Reply Quote 0
                      • Webmaster4o
                        Webmaster4o @ccc last edited by

                        @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

                        1 Reply Last reply Reply Quote 1
                        • omz
                          omz last edited by 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.

                          1 Reply Last reply Reply Quote 0
                          • abcabc
                            abcabc last edited by

                            @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

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post
                            Powered by NodeBB Forums | Contributors