Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MTK-generated ODEFunction evaluates more slowly than hand-written function #3338

Open
vyudu opened this issue Jan 17, 2025 · 0 comments
Open
Labels
bug Something isn't working

Comments

@vyudu
Copy link
Contributor

vyudu commented Jan 17, 2025

Seems like the MTK-generated is ~5x slower than a corresponding hand-written function

using ModelingToolkit
using BenchmarkTools
using SciMLBase
t = ModelingToolkit.t_nounits; D = ModelingToolkit.D_nounits

@parameters α β γ δ
@variables x(t) y(t)
    
eqs = [D(x) ~ α * x - β * x * y,
       D(y) ~ -γ * y + δ * x * y]
    
@mtkbuild sys = ODESystem(eqs, t)
f1 = ODEFunction(sys)
f2 = ODEFunction(sys; eval_expression = true)
f3 = ODEFunction{true, SciMLBase.FullSpecialize}(sys)
f4 = ODEFunction{true, SciMLBase.FullSpecialize}(sys; eval_expression = true)

function lotkavolterra!(du, u, p, t) 
    du[1] = p[1]*u[1] - p[2]*u[1]*u[2]
    du[2] = -p[4]*u[2] + p[3]*u[1]*u[2]
end

du = zeros(2); u = rand(2); p = rand(4); t_ = 0.

@btime f1(du,u,p,t_) # 93.335 ns
du = zeros(2)
@btime f2(du,u,p,t_) # 92.068 ns
du = zeros(2)
@btime f3(du,u,p,t_) # 88.889 ns
du = zeros(2)
@btime f4(du,u,p,t_) # 85.842 ns
du = zeros(2)
@btime lotkavolterra!(du,u,p,t_) # 17.368 ns
@vyudu vyudu added the bug Something isn't working label Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant