I would like to benchmark a specific code segment inside a for loop in Python. 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 is this is solved?
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)
Try this:
code='etok=utils.token("%s","%s","%s")' % (filename, keypath, i)
This will allow you to create a code string that has the values you want. Also, by using the %s
conversion, i
is coerced into a str
type for you.
Edit: Added double quotes around values.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.