I need to check the runtime of a function using the timeit
module of Python 3, however it does not accept that I pass the value inside the function by a variable.
If I replace the line:
tempo = timeit.timeit("comp_string(string, 'Fred')", "from __main__ import
by
tempo = timeit.timeit("comp_string('Fred', 'Fred')", "from __main__ import
It works, but passing the value Fred
to the variable does not work.
How can I fix this? Here is the code:
'''
Comparação de Strings por Força Bruta
'''
import timeit
def comp_string(string_new, string_origin):
for i, c in enumerate(string_origin):
if string_new[i] != c:
return False
return True
def calcula_exec(string):
#Calcula tempo de execução do código
tempo = timeit.timeit("comp_string(string, 'Fred')", "from __main__ import comp_string")
return tempo