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

Chris Angelico rosuav at gmail.com
Mon Feb 26 06:40:36 EST 2018


On Mon, Feb 26, 2018 at 10:13 PM, bartc <bc at freeuk.com> wrote:
> Below is the first draft of a Python port of a program to do with random
> numbers. (Ported from my language, which in turned ported it from a C
> program by George Marsaglia, the random number guy.)
>
> However, running it quickly exhausts the memory in my machine. The reason is
> that Python unhelpfully widens all results to bignums as needed. The code
> relies on calculations being modulo 2^64.
>
> Note that restricting integer ops to 64 bits probably still won't work, as I
> believe the numbers need to be unsigned.

No, it's because the original implementation assumed integer
wrap-around (at least, I think that's what's happening; I haven't
analyzed the code in great detail). That means all your integer
operations are doing two jobs: the one they claim to, and then a
masking to 64 bits signed. That's two abstract operations that happen,
due to the nature of the CPU, to work efficiently together. If you
don't implement both halves of that in your Python port, you have
failed at porting. What if you were porting a program from a 72-bit
chip that assumed Binary Coded Decimal? Would you complain that C's
integers are badly behaved?

And that's without even asking whether a C program that assumes
integer wrap-around counts as portable. At least with Python, you have
a guarantee that integer operations are going to behave the same way
on all compliant implementations of the language.

ChrisA



More information about the Python-list mailing list