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. hp -- _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | h...@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!"
signature.asc
Description: PGP signature
-- https://mail.python.org/mailman/listinfo/python-list