How to make Python run as fast (or faster) than Julia
Terry Reedy
tjreedy at udel.edu
Sat Feb 24 00:18:58 EST 2018
On 2/23/2018 11:32 AM, Python wrote:
> On Fri, Feb 23, 2018 at 03:11:36AM -0500, Terry Reedy wrote:
>>>> Why do you care about the 50 million calls? That's crazy -- the important
>>>> thing is *calculating the Fibonacci numbers as efficiently as possible*.
>>
>>> If you are writing practical programs, that's true. But the Julia
>>> benchmarks are not practical programs; they are designed to compare
>>> the performance of various language features across a range of
>>> languages.
>>
>> If that were so, then the comparison should use the fastest *Python*
>> implementation.
>
> Doing that would completely fail to accomplish the task of comparing
> the performance of recursive function calls in the two languages,
> which is what the benchmark was designed to do.
That is an non goal because *languages* do not have clock-time speeds,
only programs written in concrete implementations run on real hardware.
Why do you think it fair to compare function call times using the
slowest rather than the fastest implementation of Python function calls?
Real Python programmers who are seriously concerned about time try to
use the fastest implementation appropriate to the situation.
> So, no actually, it shouldn't.
To me, it is 'not using the fasted Python' that fails to make apples to
apples comparisons.
It has been said here that comparisons should use the same algorithm
doing the much the same work in both implementation. However, a Python
function call does *much more work* than in most languages, because the
meaning of 'function call' is much more flexible in Python than most
languages. The usual comparison is like lemons (other language calls)
to oranges (Python language calls, much more flexible). To be fair, the
comparison should be to a Python implementation that either notices or
accepts a declaration that, for instance, fib(n) only needs to pass an
int by position.
Comparing int addition to python object addition also compares unlike
operations. To compare using the same addition algorithm, one must use
an implementation that can restrict '+' to just int addition.
The Juila fib benchmark uses the type of function call Julia is good at.
Suppose we define fib in a way that uses Python features.
def fib(n, dummy):
if n >= 2:
return fib(n=n-1, dummy=dummy) + fib(dummy=dummy, n=n-2)
elif n >= 0:
return 1
else:
return None, n # or some error indicator including the bad value
If the language does not support 'parameter=value' syntax (which existed
long before Python), use ('n', n) and ('dummy', dummy) instead.
Now let's compare call times.
Or lets try 'fib(n-1, dummy) + fib(dummy=dummy, n=n-2)' to compare
functions that can identify argments either by position or by name.
Or f(n-1, dummy) + f(dummy=dummy, n=n-2) + f(*(n-3, dummy)), and change
'2' to '3', to utilize another Python call feature. If the language
does not support *args, omit '*'.
--
Terry Jan Reedy
More information about the Python-list
mailing list