循环或同一命令n次
我目前正在尝试清理/改进我最终工作的某些代码。
两者中的哪个速度更快,并且在一系列的次数中编写的同一命令数量越快。
例如。
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倍:
- 计数最初设置为零
- 评估计数小于10(0)
- 您好打印
- 计数被一个
增加- 虽然评估计数小于10(1)
- 您好打印
- 计数被一个
增加- 虽然评估计数小于10(2)
- 您好打印
- 计数被一个
增加- 虽然评估计数小于10(3)
- 您好打印
- 计数被一个
增加- 虽然评估计数小于10(4)
- 您好打印
- 计数被一个
增加- 虽然评估计数小于10(5)
- 您好打印
- 计数被一个
增加- 虽然评估计数小于10(6)
- 您好打印
- 计数被一个
增加- 虽然评估计数小于10(7)
- 您好打印
- 计数被一个
增加- 虽然评估计数小于10(8)
- 您好打印
- 计数被一个
增加- 虽然评估计数小于10(9)
- 您好打印
- 计数被一个
增加- 虽然评估数量不再少于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:
- count is initially set to zero
- while evaluates count to be less than 10 (0)
- hello is printed
- count is incremented by one
- while evaluates count to be less than 10 (1)
- hello is printed
- count is incremented by one
- while evaluates count to be less than 10 (2)
- hello is printed
- count is incremented by one
- while evaluates count to be less than 10 (3)
- hello is printed
- count is incremented by one
- while evaluates count to be less than 10 (4)
- hello is printed
- count is incremented by one
- while evaluates count to be less than 10 (5)
- hello is printed
- count is incremented by one
- while evaluates count to be less than 10 (6)
- hello is printed
- count is incremented by one
- while evaluates count to be less than 10 (7)
- hello is printed
- count is incremented by one
- while evaluates count to be less than 10 (8)
- hello is printed
- count is incremented by one
- while evaluates count to be less than 10 (9)
- hello is printed
- count is incremented by one
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果有疑问:测量。
什么更好?
导入dis
并比较bytecode导入TimeIt
并比较Runtimes这样的
能力
。 >奇怪的土地中的陌生人 source 。
²)
If in doubt: measure.
What is better?
import dis
and compare bytecodeimport timeit
and compare runtimesimport this
² as code ;o)like so:
to get
¹) Coined by Robert A. Heinlein in his science fiction novel Stranger in a Strange Land source.
²) The Zen of Python