简体   繁体   English

将 function 传递给 python 中的 cProfile 的正确方法是什么?

[英]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.我正在尝试使用cProfilepstats库对我的代码进行基准测试。 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.我正在尝试对gillespie_tau_leaping function 进行基准测试,它的输入都是 arrays 除了propensity_calc是一个epsidelta_t的常量。

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))上线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.我之前遇到过类似的问题,我实际上并没有将 function 作为参数传递,而是调用 function 并将结果(这是一个元组)传递给内置 ZC1C4252068E683894F14B7 的文档function 被传递给它。

Is it the same problem here, and if so how do I fix it (I never figured it out last time)这里是不是同样的问题,如果是,我该如何解决(我上次没弄明白)

Cheers干杯

For this, and most functions like it, you pass the function itself, uncalled, then the arguments to the function, as arguments to runcall ; 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 ). 它被记录为采用func ,然后是 varargs 和 kwargs ( *args**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:您所做的只是删除 function 的调用括号,并在 function 之后放置一个逗号,以将其与 arguments 分开:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM