0

I am currently trying to cleanup/improve on some code I finally got working.

Which of the two is faster between a while loop and the same command written over and over a set number of times.

EG.

count = 0
while count < 10:
    print('hello')
    count += 1

OR

print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')

The while loop is cleaner but is it faster? I am still quite new to this so my understanding is that in terms of the above procedural code, the while loop will run 32 statements in total compared to the print only statements which would run 10 times only:

  1. count is initially set to zero
  2. while evaluates count to be less than 10 (0)
  3. hello is printed
  4. count is incremented by one
  5. while evaluates count to be less than 10 (1)
  6. hello is printed
  7. count is incremented by one
  8. while evaluates count to be less than 10 (2)
  9. hello is printed
  10. count is incremented by one
  11. while evaluates count to be less than 10 (3)
  12. hello is printed
  13. count is incremented by one
  14. while evaluates count to be less than 10 (4)
  15. hello is printed
  16. count is incremented by one
  17. while evaluates count to be less than 10 (5)
  18. hello is printed
  19. count is incremented by one
  20. while evaluates count to be less than 10 (6)
  21. hello is printed
  22. count is incremented by one
  23. while evaluates count to be less than 10 (7)
  24. hello is printed
  25. count is incremented by one
  26. while evaluates count to be less than 10 (8)
  27. hello is printed
  28. count is incremented by one
  29. while evaluates count to be less than 10 (9)
  30. hello is printed
  31. count is incremented by one
  32. while evaluates count to no longer be less than 10 (10) While loop breaks out and processing ends

Based on the above, I would assume that the advantage of the while loop is neatness in code and speed of writing that code (not necessarily the speed in execution, albeit with computers been so powerful, one wouldn't notice.)

Am I correct in the above assumption?

EDIT: That was quick, I see some answers confirming my initial thoughts about optimization. Thanks.

The above code example is not related to my project, its just to show understanding.

David
  • 127
  • 3
  • 10
  • 2
    `print( *("hello" for _ in range(10)), sep="\n")` - it is just one print command. The slow thing are the multiple prints - altough they flush when they want so its not too bad – Patrick Artner Apr 26 '22 at 15:44
  • 2
    Seems like you're asking about [loop unrolling](https://stackoverflow.com/questions/2349211/when-if-ever-is-loop-unrolling-still-useful) which is a well-known practice and has its uses; however in Python you're not going to see any improvement if you unroll loops in the Python code itself, since Python is not compiled to native code before being executed. If you're really really concerned about optimization, then don't use Python, and even then, don't [prematurely optimize](https://softwareengineering.stackexchange.com/q/80084) unless there's a bottleneck in your program. – Random Davis Apr 26 '22 at 15:44
  • 9
    [Premature optimization is the root of all evil](http://c2.com/cgi/wiki?PrematureOptimization). Do whatever you feel is most natural and readable, and optimize it if it becomes a performance bottleneck. – Barmar Apr 26 '22 at 15:44
  • @PatrickArtner, thanks for that code. Will se in the future for similar iterations. Will have to learn about flushing as I have briefly come across it – David Apr 26 '22 at 15:50
  • @RandomDavis, thanks for linking the loop rolling post. I had a quick look but I see there is talk about cpu cycles on different cpus. Will have to read into that post more thoroughly – David Apr 26 '22 at 15:55
  • @Barmar. Thanks , my code is definately running fast for now until I add other sections it. Was just concerned about code length as some sections dont follow 'DRY' but I am also of the stance that it has a more natural feel. – David Apr 26 '22 at 16:00

1 Answers1

1

If in doubt: measure.

What is better?

  • fewer bytecode? ==> import dis and compare bytecode
  • faster execution ==> import timeit and compare runtimes
  • readability ==> depends on your weakest cowororkers ability to grok¹ your code (run a file with import this² as code ;o)

like so:

import dis
import timeit

def a():
    count = 0
    p = print  # local caching of global lookup - way faster
    while count < 10:
        p('hello', flush=True)
        count += 1

def b():
    p = print  # local caching of global lookup - way faster
    p('hello', flush=True)
    p('hello', flush=True)
    p('hello', flush=True)
    p('hello', flush=True)
    p('hello', flush=True)
    p('hello', flush=True)
    p('hello', flush=True)
    p('hello', flush=True)
    p('hello', flush=True)
    p('hello', flush=True)

def c():
    print( *("hello" for _ in range(10)), sep="\n", flush=True)

aa = timeit.timeit(a, number=100)
bb = timeit.timeit(b, number=100)
cc = timeit.timeit(c, number=100)
print(aa, bb, cc, sep="\n")

dis.dis(a)
print("-"*80)
dis.dis(b)
print("-"*80)
dis.dis(c)

to get

0.059448799999699986  # while is faster - who would have thought
0.06415420000030281   # multiple prints
0.06454990000020189   # one print but list comp

# while loop  - seems to be shortest
  5           0 LOAD_CONST               1 (0)
              2 STORE_FAST               0 (count)

  6           4 LOAD_GLOBAL              0 (print)
              6 STORE_FAST               1 (p)

  7           8 LOAD_FAST                0 (count)
             10 LOAD_CONST               2 (10)
             12 COMPARE_OP               0 (<)
             14 POP_JUMP_IF_FALSE       24 (to 48)

  8     >>   16 LOAD_FAST                1 (p)
             18 LOAD_CONST               3 ('hello')
             20 LOAD_CONST               4 (True)
             22 LOAD_CONST               5 (('flush',))
             24 CALL_FUNCTION_KW         2
             26 POP_TOP

  9          28 LOAD_FAST                0 (count)
             30 LOAD_CONST               6 (1)
             32 INPLACE_ADD
             34 STORE_FAST               0 (count)

  7          36 LOAD_FAST                0 (count)
             38 LOAD_CONST               2 (10)
             40 COMPARE_OP               0 (<)
             42 POP_JUMP_IF_TRUE         8 (to 16)
             44 LOAD_CONST               0 (None)
             46 RETURN_VALUE
        >>   48 LOAD_CONST               0 (None)
             50 RETURN_VALUE
--------------------------------------------------------------------------------
# multiple prints
 11           0 LOAD_GLOBAL              0 (print)
              2 STORE_FAST               0 (p)

 12           4 LOAD_FAST                0 (p)
              6 LOAD_CONST               1 ('hello')
              8 LOAD_CONST               2 (True)
             10 LOAD_CONST               3 (('flush',))
             12 CALL_FUNCTION_KW         2
             14 POP_TOP

 13          16 LOAD_FAST                0 (p)
             18 LOAD_CONST               1 ('hello')
             20 LOAD_CONST               2 (True)
             22 LOAD_CONST               3 (('flush',))
             24 CALL_FUNCTION_KW         2
             26 POP_TOP

 14          28 LOAD_FAST                0 (p)
             30 LOAD_CONST               1 ('hello')
             32 LOAD_CONST               2 (True)
             34 LOAD_CONST               3 (('flush',))
             36 CALL_FUNCTION_KW         2
             38 POP_TOP

 15          40 LOAD_FAST                0 (p)
             42 LOAD_CONST               1 ('hello')
             44 LOAD_CONST               2 (True)
             46 LOAD_CONST               3 (('flush',))
             48 CALL_FUNCTION_KW         2
             50 POP_TOP

 16          52 LOAD_FAST                0 (p)
             54 LOAD_CONST               1 ('hello')
             56 LOAD_CONST               2 (True)
             58 LOAD_CONST               3 (('flush',))
             60 CALL_FUNCTION_KW         2
             62 POP_TOP

 17          64 LOAD_FAST                0 (p)
             66 LOAD_CONST               1 ('hello')
             68 LOAD_CONST               2 (True)
             70 LOAD_CONST               3 (('flush',))
             72 CALL_FUNCTION_KW         2
             74 POP_TOP

 18          76 LOAD_FAST                0 (p)
             78 LOAD_CONST               1 ('hello')
             80 LOAD_CONST               2 (True)
             82 LOAD_CONST               3 (('flush',))
             84 CALL_FUNCTION_KW         2
             86 POP_TOP

 19          88 LOAD_FAST                0 (p)
             90 LOAD_CONST               1 ('hello')
             92 LOAD_CONST               2 (True)
             94 LOAD_CONST               3 (('flush',))
             96 CALL_FUNCTION_KW         2
             98 POP_TOP

 20         100 LOAD_FAST                0 (p)
            102 LOAD_CONST               1 ('hello')
            104 LOAD_CONST               2 (True)
            106 LOAD_CONST               3 (('flush',))
            108 CALL_FUNCTION_KW         2
            110 POP_TOP

 21         112 LOAD_FAST                0 (p)
            114 LOAD_CONST               1 ('hello')
            116 LOAD_CONST               2 (True)
            118 LOAD_CONST               3 (('flush',))
            120 CALL_FUNCTION_KW         2
            122 POP_TOP
            124 LOAD_CONST               0 (None)
            126 RETURN_VALUE
--------------------------------------------------------------------------------

# fancy generator comprehension
 24           0 LOAD_GLOBAL              0 (print)
              2 LOAD_CONST               1 (<code object <genexpr> at 0x000002320A8468C0, file "c:\Users\partner\Documents\Coding\python\t.py", line 24>)
              4 LOAD_CONST               2 ('c.<locals>.<genexpr>')
              6 MAKE_FUNCTION            0
              8 LOAD_GLOBAL              1 (range)
             10 LOAD_CONST               3 (10)
             12 CALL_FUNCTION            1
             14 GET_ITER
             16 CALL_FUNCTION            1
             18 LOAD_CONST               4 ('\n')
             20 LOAD_CONST               5 (True)
             22 LOAD_CONST               6 (('sep', 'flush'))
             24 BUILD_CONST_KEY_MAP      2
             26 CALL_FUNCTION_EX         1
             28 POP_TOP
             30 LOAD_CONST               0 (None)
             32 RETURN_VALUE

Disassembly of <code object <genexpr> at 0x000002320A8468C0, file "c:\Users\partner\Documents\Coding\python\t.py", line 24>:
              0 GEN_START                0

 24           2 LOAD_FAST                0 (.0)
        >>    4 FOR_ITER                 5 (to 16)
              6 STORE_FAST               1 (_)
              8 LOAD_CONST               0 ('hello')
             10 YIELD_VALUE
             12 POP_TOP
             14 JUMP_ABSOLUTE            2 (to 4)
        >>   16 LOAD_CONST               1 (None)
             18 RETURN_VALUE

¹) Coined by Robert A. Heinlein in his science fiction novel Stranger in a Strange Land source.

²) The Zen of Python

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Wow, this is truly something. Great advise on the 'if in doubt measure'. I just wasn't sure how to it, but now I have learn't something new. Will try going through the code to get a better understanding, but it is working as my results are 0.01919340000313241, 0.014967499999329448, 0.013026900000113528. Thank you very much – David Apr 26 '22 at 16:08
  • Though, on my side it seems like def c(): is fastest :), so I guess it also depends on cpu as per @randomdavis's 'loop rolling' post. Sill a great way test performance based on which system the code will be runnning, so thanks again – David Apr 26 '22 at 16:12