Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function definition in Python takes a lot of time

Why is python trying to calculate the value of p during definition? It takes ages to define this function.

def f():
    raise Exception('Some error')
    p = 2322111239**42322222334923492304923

print 'Defined!'

Also if the value of p is being calculated during definition, why is it possible to define this function without errors?

def f():
    return 4
    p = 11/0

This one obviously works fine because constants are not involved:

def f():
    raise Exception('Some error')
    x=42322222334923492304923
    p = 2322111239**x

print 'Defined!'
like image 901
Piotr Dabkowski Avatar asked Jun 02 '14 23:06

Piotr Dabkowski


1 Answers

It is the peephole optimizer:

https://github.com/python/cpython/blob/2.7/Python/peephole.c#L88

See in particular lines 104-106:

case BINARY_POWER:
    newconst = PyNumber_Power(v, w, Py_None);
    break;

The intention is to speed up runtime execution of the function, trading-off for a slower definition time when the module is imported. The idea is you only need to compile the code for the function once, but you may need to call it many times, and the outcome of an exponentiation binop for two constants is not going to change so it needn't be recomputed every time.

Note: In Python 3, constant folding has moved into the new AST optimizer in ast_opt.c, peephole.c is gone. The code now has safeguards in place to prevent the overly-eager optimizations that could cause as a slow or memory-hungry parse/compile step as shown in this question.

like image 50
wim Avatar answered Sep 22 '22 11:09

wim