3 ms·
>But if I ask Python to [add up about a hundred million] [32-bit floating point numbers in the naive way], Python takes about twenty seconds anyway Really?
by zahlman 1y ago
>But if I ask Python to [add up about a hundred million] [32-bit floating point numbers in the naive way], Python takes about twenty seconds anyway
Really?
$ python -m timeit -s 'import random; x = [random.random() for _ in range(100000000)]' 'sum(x)'
1 loop, best of 5: 806 msec per loop
This is on 11-year-old hardware. Even the generation isn't that slow:
$ time python -c 'import random; x = [random.random() for _ in range(100000000)]'
real 0m10.942s
user 0m9.590s
sys 0m1.346s
Of course, Python forces the use of doubles internally. Any optimizations inherent to 32-bit floats are simply not available.