Mike5298
Mike5298

Reputation: 385

Whats the correct way to pass a function to cProfile in python?

I'm trying to benchmark my code using the cProfile and pstats libraries. This is the code I've got so far:

profile = cProfile.Profile()
profile.runcall(gillespie_tau_leaping(propensity_calc, popul_num, LHS, stoch_rate, popul_num_all, tao_all, rxn_vector, delta_t, tao, epsi))
ps = pstats.Stats(profile)
ps.print_stats()

I'm trying to benchmark the gillespie_tau_leaping function whose inputs are all arrays except for propensity_calc which is an function, epsi and delta_t which are constants.

Only at the moment I'm getting the following error:

File "c:/Users/Mike/visual studio code project/MSc dissertation code/tau_leaping_variant_ssa.py", line 190, in <module>
profile.runcall(gillespie_tau_leaping(propensity_calc, popul_num, LHS, stoch_rate, popul_num_all, tao_all, rxn_vector, delta_t, tao, epsi))
TypeError: 'tuple' object is not callable

On the line profile.runcall(gillespie_tau_leaping(propensity_calc, popul_num, LHS, stoch_rate, popul_num_all, tao_all, rxn_vector, delta_t, tao, epsi))

I've had a similar problem before where I wasn't actually passing the function as an argument instead I was calling the function and passing the results (which was a tuple) to the built-in function, which according to the documentation requires a function to be passed to it.

Is it the same problem here, and if so how do I fix it (I never figured it out last time)

Cheers

Upvotes: 1

Views: 1896

Answers (1)

ShadowRanger
ShadowRanger

Reputation: 155363

For this, and most functions like it, you pass the function itself, uncalled, then the arguments to the function, as arguments to runcall; it's documented to take a func, followed by varargs and kwargs (*args and **kwargs). All you change is removing the call parens for the function, and putting a comma after the function to separate it from its arguments:

profile.runcall(gillespie_tau_leaping, propensity_calc, popul_num, LHS, stoch_rate, popul_num_all, tao_all, rxn_vector, delta_t, tao, epsi)

Upvotes: 3

Related Questions