[Python-Dev] Program runs in 12s on Python 2.7, but 5s on Python 3.5 -- why so much difference?

Ben Hoyt benhoyt at gmail.com
Tue Jul 18 21:41:38 EDT 2017


Thanks, Nick -- that's interesting. I just saw the extra JUMP_FORWARD and
JUMP_ABSOLUTE instructions on my commute home (I guess those are something
Python 3.x optimizes away).

VERY strangely, on Windows Python 2.7 is faster! Comparing 64-bit Python
2.7.12 against Python 3.5.3 on my Windows 10 laptop:

* Python 2.7.12: 4.088s
* Python 3.5.3: 5.792s

I'm pretty sure MSVC/Windows doesn't support computed gotos, but that
doesn't explain why 3.5 is so much faster than 2.7 on Mac. I have yet to
try it on Linux.

-Ben

On Tue, Jul 18, 2017 at 9:35 PM, Nick Coghlan <ncoghlan at gmail.com> wrote:

> On 19 July 2017 at 02:18, Antoine Pitrou <solipsis at pitrou.net> wrote:
> > On Tue, 18 Jul 2017 12:03:36 -0400
> > Ben Hoyt <benhoyt at gmail.com> wrote:
> >> The program is a pentomino puzzle solver, and it works via code
> generation,
> >> generating a ton of nested "if" statements, so I believe it's exercising
> >> the Python bytecode interpreter heavily.
> >
> > A first step would be to see if the generated bytecode has changed
> > substantially.
>
> Scanning over them, the Python 2.7 bytecode appears to have many more
> JUMP_FORWARD and JUMP_ABSOLUTE opcodes than appear in the 3.6 version
> (I didn't dump them into a Counter instance to tally them properly
> though, since 2.7's dis module is missing the structured opcode
> iteration APIs).
>
> With the shift to wordcode, the overall size of the bytecode is also
> significantly *smaller*:
>
> >>> len(co.co_consts[0].co_code) # 2.7
> 14427
>
> >>> len(co.co_consts[0].co_code) # 3.6
> 11850
>
> However, I'm not aware of any Python profilers that currently offer
> opcode level profiling - the closest would probably be VMProf's JIT
> profiling, and that aspect of VMProf is currently PyPy specific
> (although could presumably be extended to CPython 3.6+ by way of the
> opcode evaluation hook).
>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/
> benhoyt%40gmail.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20170718/e3099201/attachment.html>


More information about the Python-Dev mailing list