几天后我有一个Python测试。
其中一个测试题是关于运行时间的问题,给我们一个代码,问我们运行时间是多少。
我想问是否有可能知道运行时间是通过打印的东西?
因为在测试中,我们可以选择只使用打印来编写代码和调试。
例如:问题=
what is the time complexity of this code?
def func( n ):
i=7
j = 2**n + n
while (i < j):
i *= 2
j //= 2
c=i
for k in range(int (c ** (1/2))):
if (k > c**(1/4)) :
break
添加一个计数器,在每个循环的每次迭代中增加它,并打印它。
def func( n ):
i=7
j = 2**n + n
ops = 1
while (i < j):
ops += 1
i *= 2
j //= 2
c=i
for k in range(int (c ** (1/2))):
ops += 1
if (k > c**(1/4)) :
break
print(ops)
在一个范围内执行此操作,例如0
for i in range(100):
func(i)