curious curious - 3 months ago 12
Python Question

Python loop benchmark with timeit

I would like to benchmark a specific code segment inside a for loop in pytohn. I am using timeit as follows:

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)))


However this raises:

NameError: global name 'filename' is not defined


when I include filename in setup variable Python says:

ImportError: cannot import name filename


How this is solved?

Answer

filename isn't defined in the scope of the code in the timeit block. I don't know what utils is in your code, but assuming it expects filename and keypath as strings just replace your

    code='etok=utils.token(filename,keypath,str(i))'

line with:

    code='etok=utils.token("{}","{}",{})'.format(filename, keypath, i)