question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

pyjulia slower than expected for some operations

See original GitHub issue

I’ve been using pyjulia to wrap a julia package, but the wrapper seems slower than I would expect. The pyjulia code using diffeqpy involves some setup to generate the react object which has a julia ODEProblem with a julia function attached, but the main operation is a solve like this:

sol = de.solve(react.ode,de.CVODE_BDF(),abstol=1e-16,reltol=1e-6)

In pure julia at first evaluation this takes about 20 seconds, and after that it takes about 6 seconds In pyjulia at first evaluation this takes about 30 seconds and after that it takes about 18 seconds.

I get the same result if I interface directly with DifferentialEquations without diffeqpy.

I can’t think of any reason it should be that different. Does some of the jit compilation have to happen every evaluation in pyjulia? I’m using the python-jl fix if that affects things.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
tkfcommented, Sep 3, 2020

If you use PyJulia for passing around Julia functions and values, it should be as fast as pure Julia solution (if the main computation is done in Julia). My guess is that there are some quirks in how PyCall wrap/unwrap things (or usage of it) and the ODE solver accidentally calls Julia functions via Python. I’d run Julia profiler to see if/where PyCall shows up in the hot loop.

0reactions
ChrisRackauckascommented, Sep 6, 2020

It looks like the difference has really dropped. It could’ve been a WSL problem before.

from julia.api import Julia
jl = Julia(compiled_modules=False)
from diffeqpy import de
from julia import 
import timeit

import numpy as np
jul_f = Main.eval("""
function f(du,u,p,t)
  x, y, z = u[1], u[2], u[3]
  sigma, rho, beta = p[1], p[2], p[3]
  du[1] = sigma * (y - x)
  du[2] = x * (rho - z) - y
  du[3] = x * y - beta * z
end""")
u0 = [1.0,0.0,0.0]
tspan = (0., 100.)
t = np.linspace(0, 100, 1001)
p = [10.0,28.0,8/3]
prob = de.ODEProblem(jul_f, u0, tspan, p)
sol = de.solve(prob,saveat=t,abstol=1e-8,reltol=1e-8)
 
def time_func():
    sol = de.solve(prob,saveat=t,abstol=1e-8,reltol=1e-8)
 
time_func()
timeit.Timer(time_func).timeit(number=10000)/10000 # 0.003481317869999998 seconds

vs

using DifferentialEquations, BenchmarkTools
function lorenz!(du,u,p,t)
    du[1] = p[1]*(u[2]-u[1])
    du[2] = u[1]*(p[2]-u[3]) - u[2]
    du[3] = u[1]*u[2] - p[3]*u[3]
end
u0 = [1.0;0.0;0.0]
p = [10.0,28.0,8/3]
tspan = (0.0,100.0)
prob = ODEProblem(lorenz!,u0,tspan,p)
@btime solve(prob,Tsit5(),saveat=0.1,reltol=1e-8,abstol=1e-8) # 3.283 ms (1055 allocations: 154.44 KiB)

That last amount could just be simple setup stuff, so this can be closed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Performance · Issue #133 · JuliaPy/pyjulia - GitHub
At the moment, calling j.sin is 10x slower than math.sin or numpy.sin. I'm guessing that this is due to type inference issues with...
Read more >
Troubleshooting — PyJulia 0.6.0 documentation
This is because those Python executables are statically linked to libpython. ... Note that this option slows down loading and using Julia packages ......
Read more >
Julia seems an order of magnitude slower than Python when ...
The thing is that Julia is several orders of magnitude slower than Python to print out to the terminal. Here's the timing result...
Read more >
JNumPy: Writing high-performance C extensions for Python in ...
Another scenario is that you want to write some custom code which makes use of ... expect that the extension compiles to C...
Read more >
Running scripts is terribly slow. I'm new to Julia, so maybe I'm ...
I just started learning Julia about a week ago. I read through plenty of the documentation on how to start on Julia, and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found