Debugging reason for python running unreasonably slow when adding numbers
Thomas Passin
list1 at tompassin.net
Tue Mar 14 15:27:35 EDT 2023
On 3/14/2023 3:48 AM, 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:
>
> ```python
> import random, time
> from functools import reduce
>
> def trainPerceptron(perceptron, data):
> learningRate = 0.002
> weights = perceptron['weights']
> error = 0
> for chunk in data:
> input = chunk['input']
> output = chunk['output']
>
> # 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)
>
> actualOutput = sum_ if sum_ > 0 else 0
>
> expectedOutput = 1 if output == perceptron['id'] else 0
> currentError = expectedOutput - actualOutput
> if currentError:
> error += currentError ** 2
> change = currentError * learningRate
> for key in input:
> weights[key] += change
[snip]
Just a speculation, but the difference with the javascript behavior
might be because the JS JIT compiler kicked in for these loops.
More information about the Python-list
mailing list