Ask Your Question

Revision history [back]

We should inspect further, but there is indeed an issue. Thanks for reporting It has to do with the symbolic computations. Indeed, when you write 2*sqrt(k*log(log(k+1))), since k+1 is an integer, the computation is done symbolically, and not numerically, which takes more time. The quick way to avoid this is to add a dot . after the 1 to specify that it is a floating-point number and not an integer (anyway, you will compare with a float at the end):

So, just replace :

        if data[k] > 2*sqrt(k*log(log(k+1))):

with

        if data[k] > 2*sqrt(k*log(log(k+1.))):

What i do not understant though, is that this solution, while much faster than the initial code (and not leaking anymore), is still much slower than running the code from ipython (and adding the line from math import sqrt, log at the beginning), while the only remaining difference is about coercion.

We should inspect further, but there is indeed an issue. Thanks for reporting ! It has to do with the symbolic computations. Indeed, when you write 2*sqrt(k*log(log(k+1))), since k+1 is an integer, the computation is done symbolically, and not numerically, which takes more time. The quick way to avoid this is to add a dot . after the 1 to specify that it is a floating-point number and not an integer (anyway, you will compare with a float at the end):

So, just replace :

        if data[k] > 2*sqrt(k*log(log(k+1))):

with

        if data[k] > 2*sqrt(k*log(log(k+1.))):

What i do not understant though, is that this solution, while much faster than the initial code (and not leaking anymore), is still much slower than running the code from ipython (and adding the line from math import sqrt, log at the beginning), while the only remaining difference is about coercion.