r/badcode • • Apr 30 '22

python Looks like Python List Comprehension-phobia is a thing. This is a real commit by a teammate.

Post image
1.2k Upvotes

125 comments sorted by

View all comments

Show parent comment

6

u/[deleted] May 01 '22 edited May 01 '22

Seems to be marginal improvement at times.

Comprehensions vs for loops, not what you think

16

u/SV-97 May 01 '22

"At times" - yes. But even in very simple cases the difference can be quite large (20% difference when computing a list of squares from 0 to 100 [well 99]).

Bytecode for loop-based code of the sample I ran:

  1           0 BUILD_LIST               0
			  2 STORE_NAME               0 (lst)

  2           4 LOAD_NAME                1 (range)
			  6 LOAD_CONST               0 (100)
			  8 CALL_FUNCTION            1
			 10 GET_ITER
		>>   12 FOR_ITER                18 (to 32)
			 14 STORE_NAME               2 (x)

  3          16 LOAD_NAME                0 (lst)
			 18 LOAD_METHOD              3 (append)
			 20 LOAD_NAME                2 (x)
			 22 LOAD_CONST               1 (2)
			 24 BINARY_POWER
			 26 CALL_METHOD              1
			 28 POP_TOP
			 30 JUMP_ABSOLUTE           12
		>>   32 LOAD_CONST               2 (None)
			 34 RETURN_VALUE

and bytecode for the comprehension

  1           0 LOAD_CONST               0 (<code object <listcomp> at 0x00000229A03C3500, file "<dis>", line 1>)
			  2 LOAD_CONST               1 ('<listcomp>')
			  4 MAKE_FUNCTION            0
			  6 LOAD_NAME                0 (range)
			  8 LOAD_CONST               2 (100)
			 10 CALL_FUNCTION            1
			 12 GET_ITER
			 14 CALL_FUNCTION            1
			 16 STORE_NAME               1 (lst)
			 18 LOAD_CONST               3 (None)
			 20 RETURN_VALUE

Disassembly of <code object <listcomp> at 0x00000229A03C3500, file "<dis>", line 1>:
  1           0 BUILD_LIST               0
			  2 LOAD_FAST                0 (.0)
		>>    4 FOR_ITER                12 (to 18)
			  6 STORE_FAST               1 (x)
			  8 LOAD_FAST                1 (x)
			 10 LOAD_CONST               0 (2)
			 12 BINARY_POWER
			 14 LIST_APPEND              2
			 16 JUMP_ABSOLUTE            4
		>>   18 RETURN_VALUE

So it will basically replace every reference to the iteration variable by a load fast and the "load_name, load_method, ..., call_method, pop" sequence with a single list_append. There are of course cases where the difference doesn't end up being that big (e.g. simply modifying the example to instead only consider even values of x) - but in general it'd be stupid not to use it especially considering how it's also far more readable and concise.

2

u/[deleted] May 01 '22

Good counterpoint. I agree, they mind as well be used unless the inside starts to get verbose.

5

u/reinigenferkel May 01 '22

Link seems to be broken.

1

u/[deleted] May 01 '22

Fixed

3

u/Sentie_Rotante May 01 '22

I agree with the article if you are just doing a calculation and not saving the results to a list but the test they used to show a loop as faster then a comprehension they did nothing with the resulting value. I totally agree that if you don't need a list a list comprehension is definitely a better way to go. And numpy arrange and doing calculations on the resulting array is better.

But my argument was that a comprehension is faster when you need the values to be in a list. I think a more accurate test is just comparing a generator (range) with the for loop and append vs the comprehension with no calculation. In this base situation the comprehension is ~25% faster. There are situations where there are better tools for the job but when it is building a list from a generator and an np array isn't a point you can start at. I have not seen a situation where a loop is faster.