[Python-ideas] Vectorization [was Re: Add list.join() please]
Steven D'Aprano
steve at pearwood.info
Sat Feb 2 18:11:02 EST 2019
On Sat, Feb 02, 2019 at 03:31:29PM -0500, David Mertz wrote:
> I still haven't seen any examples that aren't already spelled 'map(fun, it)'
You might be right. But then there's nothing that map() can do that
couldn't be written as a comprehension, and nothing that you can't do
with a comprehension that can't be written as a for-loop.
And nothing that can't be written as a for-loop that couldn't be written
as a while-loop. The only loop construct we really need is a while loop.
And even that is redundant if we had GOTO.
Its not about the functionality, but expressibility and readability.
This hypothetical vectorization syntax might have a performance
advantage as well. My understanding is that Julia is able to efficiently
vectorize code, bringing it to within 10% of the speed of unrolled C
loops. It may be that CPython cannot do anything that fast, but there
may be some opportunities for optimization that we cannot apply to
for-loops or comprehensions due to the way they are defined.
But primarily it is about the readability of the code:
result = process.(vector .+ sequence) .* items
versus:
# Ouch!
result = map(operator.mul,
zip(map(process,
map(operator.add,
zip(vector, sequence)),
items))
Here's the comprehension version:
result = [a*b for a, b in zip(
[process(c) for c in
[d+e for d, e in zip(vector, sequence)]],
items)]
We can improve that comprehension a tiny bit by splitting it into
multiple steps:
temp1 = [d+e for d, e in zip(vector, sequence)]
temp2 = [process(c) for x in temp1]
result = [a*b for a, b in zip(temp2, items)]
but none of these are as elegant or readable as the vectorized syntax
result = process.(vector .+ sequence) .* items
--
Steve
More information about the Python-ideas
mailing list