Sid
Sid

Reputation: 4055

how to time multiple functions without calling time in multiple places?

I am trying to figure out how to time multiple functions(individual execution time) without adding time in multiple lines.

This is what I have tried:

func_list = [m1strat(kite, state,bnf_qty=100),nifty_m1strat(kite, nifty_state, q_h = nifty_qty_d,q_n = nifty_qty),\
         bnf_maintainpos_fut(kite, trail=0.4),nifty_maintainpos_fut(kite, trail=0.4),bank_nifty_opt(kite,order_df),\
         nifty_opt(kite,order_df),maintain_pos(kite)]
def time_taken(func_list):
    import time
    for func in func_list:
        start = time.time()
        try:
            func
        except:
            traceback.print_exc()
            pass
        end = time.time()
        print('Time taken by' + func + ': '+ str(start-end))

When I try to run this I believe the functions run when at the list.

Trying to figure out how to get this working.

Thanks

Upvotes: 0

Views: 55

Answers (1)

Miriam
Miriam

Reputation: 2721

This should solve the problem you state:

func_list = [lambda: m1strat(kite, state,bnf_qty=100), 
             lambda: nifty_m1strat(kite, nifty_state, q_h = nifty_qty_d,q_n = nifty_qty),
             lambda: bnf_maintainpos_fut(kite, trail=0.4),
             lambda: nifty_maintainpos_fut(kite, trail=0.4),
             lambda: bank_nifty_opt(kite,order_df),
             lambda: nifty_opt(kite,order_df),maintain_pos(kite)]
def time_taken(func_list):
    import time
    for func in func_list:
        start = time.time()
        try:
            func
        except:
            traceback.print_exc()
            pass
        end = time.time()
        print('Time taken by' + func + ': '+ str(start-end))

The lambda keyword in python is basically just a quick way of defining a one-line function, check out this explanation to find out more.

Upvotes: 1

Related Questions