# 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.