r/programming • • May 25 '19

Making the obvious code fast

https://jackmott.github.io/programming/2016/07/22/making-obvious-fast.html
1.3k Upvotes

263 comments sorted by

View all comments

Show parent comments

9

u/not-na May 26 '19

I've tried three different variants:

sum(map(lambda x: x*x, range(32000000)))

Took 2.91s per loop.

sum(map(lambda x: x**2, range(32000000)))

Took 7.67s per loop.

sum(x*x for x in range(32000000))

Took 2.25s per loop.

All tested with an i7-7700 and Python 3.6.8 using timeit with 10 repeats.

It appears that x**2 is way slower than x*x and using a generator is a bit faster than straight sum+map.

I also noticed that during the generator benchmark, the CPU core that python was using wasn't as fully utilized as with the other variants.

8

u/Ki1103 May 26 '19

Thanks for running that.

The improvement in CPU usage comes from how lambda expressions are evaluated. Calling a lambda function will create a new stack frame each call, while a generator evaluates the expression on the same frame.

I'm on my mobile right now, but I can go into more detail later if you're interested.

3

u/thirdegree May 26 '19
>>> import dis
>>> dis.dis(x**2 for x in range(32000000))
  1           0 LOAD_FAST                0 (.0)
        >>    2 FOR_ITER                14 (to 18)
              4 STORE_FAST               1 (x)
              6 LOAD_FAST                1 (x)
              8 LOAD_CONST               0 (2)
             10 BINARY_POWER
             12 YIELD_VALUE
             14 POP_TOP
             16 JUMP_ABSOLUTE            2
        >>   18 LOAD_CONST               1 (None)
             20 RETURN_VALUE
>>> dis.dis(x*x for x in range(32000000))
  1           0 LOAD_FAST                0 (.0)
        >>    2 FOR_ITER                14 (to 18)
              4 STORE_FAST               1 (x)
              6 LOAD_FAST                1 (x)
              8 LOAD_FAST                1 (x)
             10 BINARY_MULTIPLY
             12 YIELD_VALUE
             14 POP_TOP
             16 JUMP_ABSOLUTE            2
        >>   18 LOAD_CONST               0 (None)
             20 RETURN_VALUE

I guess BINARY_POWER is significantly slower than BINARY_MULTIPLY, which pretty much makes sense.