我想基於pytohn for循環中的特定代碼段進行基準測試。我使用timeit如下:Python時鐘循環基準測試
def createTokens():
keypath=('./pickles/key.pickle')
path="./data/"
directory = os.listdir(path)
tok={}
print('create tokens..')
t=[2**4,2**5,2**6,2**7,2**8,2**9,2**10,2**12,2**14,2**16]
files=['pl_10000004','pl_10000002','pl_100000026']
for filename in files:
for i in t:
code='etok=utils.token(filename,keypath,str(i))'
t = timeit.Timer(stmt=code,setup='from __main__ import utils')
print(filename+'_'+str(i)+'.pickle')
print ('%f'%float(t.timeit(10/10)))
然而,這引起了:
NameError: global name 'filename' is not defined
當我包括設置變量的Python名說:
ImportError: cannot import name filename
該如何解決?
這可怎麼辦呢? – curious