我想测量部分代码的处理时间,为此我使用了timeit函数。但是,它从timeit函数内部返回IndentationError。
这是我的代码;
for stem, result in zip(stem_dirs, result_dirs):
code_to_measure = '''
print(stem, '\n', result)
subprocess.call(['python', './a.py', "--dir_in", stem, "--dir_out", result])
'''
proccess_time = timeit.timeit(code_to_measure)
print(proccess_time)下面是我得到的错误;
Traceback (most recent call last):
File "code_test.py", line 115, in <module>
proccess_time = timeit.timeit(code_to_measure)
File "/usr/local/lib/python3.6/timeit.py", line 233, in timeit
return Timer(stmt, setup, timer, globals).timeit(number)
File "/usr/local/lib/python3.6/timeit.py", line 123, in __init__
compile(stmtprefix + stmt, dummy_src_name, "exec")
File "<timeit-src>", line 3
print(stem, '
^
IndentationError: unexpected indent但是,下面代码中的timeit函数仍然可以正常运行;
# importing the required module
import timeit
# code snippet to be executed only once
mysetup = "from math import sqrt"
# code snippet whose execution time is to be measured
mycode = '''
def example():
mylist = []
for x in range(100):
mylist.append(sqrt(x))
'''
# timeit statement
print(timeit.timeit(setup = mysetup,
stmt = mycode,
number = 10000)) 以下是代码的输出;
0.002189640999858966我不太确定如何解决这个问题。如果你对这个问题有任何建议或解决方案,请告诉我。
非常感谢你提前这么做。
发布于 2019-11-04 14:25:41
执行缩进的两种方法是使用空格(标准规范是对一级缩进使用4个空格),或者使用制表符。确保没有将它们混合在一起。坚持其中的一个。
如果您能以.py文件的形式与我分享您的代码,我也许可以通过准确地告诉您问题出在哪里,从而帮助您更多。
https://stackoverflow.com/questions/58687952
复制相似问题