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.
- Am I doing something wrong with Symbolics.jl?
- Is there a way for me to get better performance of such derivative calculations using Symbolics.jl?