Debugging reason for python running unreasonably slow when adding numbers

Chris Angelico rosuav at gmail.com
Tue Mar 14 18:05:46 EDT 2023


On Wed, 15 Mar 2023 at 08:53, Peter J. Holzer <hjp-python at hjp.at> wrote:
>
> On 2023-03-14 16:48:24 +0900, Alexander Nestorov wrote:
> > I'm working on an NLP and I got bitten by an unreasonably slow
> > behaviour in Python while operating with small amounts of numbers.
> >
> > I have the following code:
> [...]
> >       # 12x slower than equivalent JS
> >       sum_ = 0
> >       for key in input:
> >           v = weights[key]
> >           sum_ += v
> >
> >       # 20x slower than equivalent JS
> >       #sum_ = reduce(lambda acc, key: acc + weights[key], input)
>
> Not surprising. Modern JavaScript implementations have a JIT compiler.
> CPython doesn't.
>
> You may want to try PyPy if your code uses tight loops like that.
>
> Or alternatively it may be possible to use numpy to do these operations.
>

Or use the sum() builtin rather than reduce(), which was
*deliberately* removed from the builtins. The fact that you can get
sum() without importing, but have to go and reach for functools to get
reduce(), is a hint that you probably shouldn't use reduce when sum
will work.

Naive code is almost always going to be slower than smart code, and
comparing "equivalent" code across languages is almost always an
unfair comparison to one of them.

ChrisA


More information about the Python-list mailing list