简体   繁体   English

如果我们只是使用 continue 跳过迭代,则 for 循环的时间复杂度

[英]Time Complexity of for loop if we are just skipping the iteration using continue

n = int(input())
for i in range(n):
    continue

What is the Time Complexity of the above loop?上述循环的时间复杂度是多少?

It's O(n) where n is the user input.这是O(n) ,其中n是用户输入。

Python -- at least the CPython implementation -- doesn't optimize away the loop because it has a noop body or anything like that. Python——至少是 CPython 实现——不会优化循环,因为它有一个 noop 主体或类似的东西。 Test it out yourself.自己测试一下。 Something like就像是

print("hello")
for i in range(10000000):
    continue
print("there")

exhibits a very noticeable pause.表现出非常明显的停顿。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM