循环或同一命令n次

发布于 2025-01-24 03:24:32 字数 1241 浏览 2 评论 0原文

我目前正在尝试清理/改进我最终工作的某些代码。

两者中的哪个速度更快,并且在一系列的次数中编写的同一命令数量越快。

例如。

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

还是

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

while循环更干净,但速度更快?我仍然很陌生,因此我的理解是,就上述过程代码而言,与仅打印语句相比,while loop将总共运行32个语句:只能运行10倍:

  1. 计数最初设置为零
  2. 评估计数小于10(0)
  3. 您好打印
  4. 计数被一个
  5. 增加
  6. 虽然评估计数小于10(1)
  7. 您好打印
  8. 计数被一个
  9. 增加
  10. 虽然评估计数小于10(2)
  11. 您好打印
  12. 计数被一个
  13. 增加
  14. 虽然评估计数小于10(3)
  15. 您好打印
  16. 计数被一个
  17. 增加
  18. 虽然评估计数小于10(4)
  19. 您好打印
  20. 计数被一个
  21. 增加
  22. 虽然评估计数小于10(5)
  23. 您好打印
  24. 计数被一个
  25. 增加
  26. 虽然评估计数小于10(6)
  27. 您好打印
  28. 计数被一个
  29. 增加
  30. 虽然评估计数小于10(7)
  31. 您好打印
  32. 计数被一个
  33. 增加
  34. 虽然评估计数小于10(8)
  35. 您好打印
  36. 计数被一个
  37. 增加
  38. 虽然评估计数小于10(9)
  39. 您好打印
  40. 计数被一个
  41. 增加
  42. 虽然评估数量不再少于10(10) 循环爆发并处理结束

根据上述处理结束,但我假设while循环的优势是代码和编写代码速度的整洁性(不一定是执行速度,尽管计算机是如此强大,但一个人会'请注意。)

我在上述假设中正确吗?

编辑:很快,我看到一些答案证实了我对优化的最初想法。谢谢。

上面的代码示例与我的项目无关,它只是为了显示理解。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

段念尘 2025-01-31 03:24:32

如果有疑问:测量。

什么更好?

  • 更少的字节码? ==&gt; 导入dis并比较bytecode
  • 更快的执行==&gt; 导入TimeIt并比较Runtimes
  • Rearlability ==&gt;取决于您最弱的奶牛的能力(用以为代码运行文件; o)

这样的

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)

能力

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

。 >奇怪的土地中的陌生人 source

²)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文