Symbolics.jl: Speed of derivative computations vs. SymEngine.jl

I benchmarked some derivative computations using Symbolics.jl, SymEngine.jl, and SymPy.jl. As expected, SymPy.jl was the slowest solution in my case. However, SymEngine.jl was significantly faster than Symbolics.jl. Here is an example:

julia> using Pkg; Pkg.activate(temp=true); Pkg.add(["SymEngine", "Symbolics", "BenchmarkTools"])

julia> using SymEngine, Symbolics, BenchmarkTools

julia> f(u) = u[2] / (u[1]^2 + u[2]^2)
f (generic function with 1 method)

julia> u_symbolics = @variables u1 u2

julia> expand_derivatives(( Differential(u_symbolics[1])(f(u_symbolics)) ))
-2u1*(u2 / ((u1^2 + u2^2)^2))

julia> @benchmark expand_derivatives($( Differential(u_symbolics[1])(f(u_symbolics)) ))
BenchmarkTools.Trial: 10000 samples with 1 evaluation.
 Range (min â€Ķ max):  19.477 Ξs â€Ķ  4.431 ms  ┊ GC (min â€Ķ max): 0.00% â€Ķ 99.14%
 Time  (median):     21.305 ξs              ┊ GC (median):    0.00%
 Time  (mean Âą σ):   22.915 Ξs Âą 61.455 Ξs  ┊ GC (mean Âą σ):  3.78% Âą  1.40%

     ▃█▇▄                                                      
  ▂▃▅█████▆▆▅▅▄▄▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▁▂▂▁▁▁▁▂▁▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂ ▃
  19.5 Ξs         Histogram: frequency by time        37.5 Ξs <

 Memory estimate: 10.97 KiB, allocs estimate: 233.

julia> u_symengine = @vars u1 u2
(u1, u2)

julia> diff((f(u_symengine)), (u_symengine[1]))
-2*u2*u1/(u1^2 + u2^2)^2

julia> @benchmark diff($(f(u_symengine)), $(u_symengine[1]))
BenchmarkTools.Trial: 10000 samples with 7 evaluations.
 Range (min â€Ķ max):  4.198 Ξs â€Ķ  35.084 Ξs  ┊ GC (min â€Ķ max): 0.00% â€Ķ 0.00%
 Time  (median):     4.268 ξs               ┊ GC (median):    0.00%
 Time  (mean Âą σ):   4.320 Ξs Âą 510.445 ns  ┊ GC (mean Âą σ):  0.00% Âą 0.00%

  ▄█▇▆▅▂▁▁▁▁                                                  ▂
  ████████████▆▆▅▃▅▃▃▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▁▃▁▁▁▃▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▆▅ █
  4.2 Ξs       Histogram: log(frequency) by time       6.1 Ξs <

 Memory estimate: 336 bytes, allocs estimate: 20.
  1. Am I doing something wrong with Symbolics.jl?
  2. Is there a way for me to get better performance of such derivative calculations using Symbolics.jl?
3 Likes

Open an issue. Worth profiling.

Done