How to make Python run as fast (or faster) than Julia

Ned Batchelder ned at nedbatchelder.com
Mon Feb 26 11:32:40 EST 2018


On 2/26/18 10:09 AM, Chris Angelico wrote:
> On Tue, Feb 27, 2018 at 2:02 AM, bartc <bc at freeuk.com> wrote:
>> On 26/02/2018 14:04, bartc wrote:
>>> On 26/02/2018 13:42, Ned Batchelder wrote:
>>
>>>    Well, once you notice that the
>>>> Python code had N=1e5, and the C code had N=1e9 :)   If you want to
>>>> experiment, with N=1e5, the final number should be 5255210926702073855.
>>>
>>> OK, I'll try that.
>>
>> I have that Python version working now. It's necessary to apply that masking
>> function to wherever numbers can get bigger.
>>
>> I don't know how long a 1-billion loop will take, but a 10-million loop took
>> 46 seconds on Python 3.6, and 21 seconds on PyPy 2.7 from a couple of years
>> ago. (And on Windows, which has a somewhat slower CPython than Linux.)
> You're still reimplementing the C code in Python, which is
> inefficient. Have you considered going back to the *actual algorithm*
> and implementing that idiomatically in Python? I think you'll find
> that (a) the code is more readable, and (b) the run time is much
> lower.
>
>

Don't bother: C will win this kind of race every time.

--Ned.



More information about the Python-list mailing list