Skip to content

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

@ranocha

Description

@ranocha

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?

Originally posted in https://discourse.julialang.org/t/symbolics-jl-speed-of-derivative-computations-vs-symengine-jl/68803; ChrisRackauckas told me to file an issue here

┆Issue is synchronized with this Trello card by Unito

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions