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

Nick Coghlan ncoghlan at gmail.com
Tue Jul 18 21:35:20 EDT 2017


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


More information about the Python-Dev mailing list