coderusher
coderusher

Reputation: 36

Difficulty in using Timeit module

count = 10, 100, 1000, 10000

for each value of count, i need to execute the method 5 times using timeit and print out the min value and the actual 5 values.

numstring contains four functions in it.

output should look like (a total of 16 lines):

numbers_string1, count = 10, min = 0.0001, actuals = [0.0001, 0.0002, 0.0001, ...]

numbers_string1, count = 100, min = 0.002, actuals = [0.002, 0.002, 0.003, ...]

....

numbers_string4, count = 10000, min = 0.1 actuals = [....]

For this i tried this way:

My Code:

from numstring import *
import timeit

def profile_timeit():
    funcs_list = [numbers_string1, numbers_string2, numbers_string3, num_strings4]
    for i in funcs_list:
        for count in [10, 100, 1000, 10000]:
            actuals = timeit.timeit(stmt='i(count)', number=4, setup='from __main__ import *')
            print "{0} count = {1} \t min = {2} \t actuals = {3}".format(i, count, min(actuals), actuals)
        print "\n"
if __name__ == "__main__":
    profile_timeit()

can anybody please help me out. Thanks in advance

Upvotes: 0

Views: 132

Answers (1)

user3397770
user3397770

Reputation: 17

could you try this....

from numstring import *
import timeit
def profile_timeit():
    funcs = [numbers_string1,numbers_string2,numbers_string3,num_strings4]
    count = [10,100,1000,10000]
    for func in funcs:
        for cnt in count:
            for i in xrange(5):
                t=timeit.Timer(stmt = "%s(%d)"%(func.__name__, cnt), setup = "from __main__ import %s"%(func.__name__, ))
                tms = t.repeat(repeat=5, number=1)
            print "%s, count = %d, min = %s , actuals = %s" % (func.__name__, cnt, min(tms), tms)
    pass

tell me if it is ok or not?

Upvotes: 1

Related Questions