You can use timeit module and pass the arguments in timeit's setup argument:
from timeit import timeit
inp = """
def return_words(string):
return list1, list2
return_words(string)
"""
for s in list_of_inputs:
print '{}'.format(s), '->', timeit(stmt=inp,
number=1000000,
setup="string = '{}'".format(s))
Demo :
inp = """
def return_words(string):
return [i for i in string if i.isdigit()]
return_words(string)
"""
list_of_inputs = ['inputstring1', 'inp2']
for s in list_of_inputs:
print '{}'.format(s), '->', timeit(stmt=inp,
number=1000000,
setup="string = '{}'".format(s))
Output:
inputstring1 -> 0.986068964005
inp2 -> 0.548749923706
Note that timeit also accepts a function as the first argument which is defined in your code, but you can not pass argument to it. In that case it's better to create a wrapper which will call your function with relative arguments. Read http://pythoncentral.io/time-a-python-function/ for more info.