Static typing [was Re: Python and the need for speed]

bartc bc at freeuk.com
Sun Apr 16 14:43:34 EDT 2017


On 16/04/2017 18:13, Steve D'Aprano wrote:
> On Mon, 17 Apr 2017 02:20 am, justin walters wrote:
>
>> On Sun, Apr 16, 2017 at 8:46 AM, bartc <bc at freeuk.com> wrote:
>>
>>> What were the results with Python on your machine?
>>
>>
>>
>> Well, at first I tried to implement it with a generator. I gave up on
>> waiting for the program to complete
>> after about 6 minutes.
>
> That seems... excessive.
>
> For what it's worth, on my machine (2GB RAM and 1.2GHz CPU) I can add up 100
> million ints in 14 seconds:
>
> py> with Stopwatch():
> ...     sum(range(100000000))
> ...
> 4999999950000000
> time taken: 14.032116 seconds

I'm surprised it took that long. If a 'range' is no longer expanded to a 
list, and 'sum' is built-in, isn't there a formula that can be used to 
sum a sequence? At least when the step is +1.

> factor of ten between friends?
>
> Of course the built-in sum() is a bit faster than the version Bart uses.
> It's almost like he is deliberately writing the least efficient Python code
> he can... (or maybe he just doesn't know Python very well).

Yes, it was low-level code, which is a large part of what optimising a 
language is about. You can't always write just high-level code that 
calls fast built-in routines; sometimes the language has to do some 
actual work of its own!

And the same algorithm (if you can call it that) was used in all the 
examples. That it wasn't doing anything meaningful is not important; it 
is testing the same mechanisms that would be involved in a real program.

-- 
bartc


More information about the Python-list mailing list