# Optim.jl vs scipy.optimize once again

**URL:** <https://discourse.julialang.org/t/optim-jl-vs-scipy-optimize-once-again/61661>\
**Category:** Performance\
**Tags:** optim\
**Created:** [May 23, 2021, 2:15am UTC](https://discourse.julialang.org/t/optim-jl-vs-scipy-optimize-once-again/61661 "2021-05-23T02:15:11Z")\
**Posts on this page:** 1\
**Showing post:** 52

<div class="post-metadata">

**Author:** ![rht](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rht/32/50146_2.png) [@rht](https://discourse.julialang.org/u/rht)\
**Post date:** [June 9, 2021, 1:04pm UTC](https://discourse.julialang.org/t/optim-jl-vs-scipy-optimize-once-again/61661/52 "2021-06-09T13:04:12Z")

</div>

Indeed. `SPGBox` mutated the `AA.xs0`! I have checked that LBFGSB.jl doesn’t mutate the `x0`.

- ForwardDiff and finite difference have almost the same time, consistent across optimization methods
- SPGBox is 40% faster than LBFGSB.jl, even with `copy(xs0)` inside the `@btime` loop (I use `copy(xs0)` so that the code is simpler)
- LBFGSB.jl is about 2x faster than scipy.optimize(method=‘L-BFGS-B’)

Log:

```julia
Benchmarking single function execution
  12.977 μs (1 allocation: 160 bytes)
Benchmarking PharmCat v2 (CG) using grad specified by ForwardDiff
  383.874 ms (18888 allocations: 3.41 MiB)
-6.263329327214707
Benchmarking SPGBox using grad specified by ForwardDiff
  3.363 ms (138 allocations: 31.19 KiB)
-6.263329329419884
Benchmarking PharmCat v2 (CG) using finite difference grad
  388.767 ms (34072 allocations: 4.52 MiB)
-6.26332930693809
Benchmarking SPGBox using finite difference grad
  3.330 ms (270 allocations: 40.83 KiB)
-6.263329329419884
Benchmarking LBFGSB.jl using grad specified by ForwardDiff
  5.198 ms (203 allocations: 44.95 KiB)
-6.263329329419884
Benchmarking LBFGSB.jl using finite difference grad
  5.105 ms (401 allocations: 59.48 KiB)
-6.263329329419884
Benchmarking scipy.optimize(method='L-BFGS-B') which uses finite difference grad
  10.605 ms (9197 allocations: 447.61 KiB)
-6.263329329419884

```

Fixed in [[BUGFIX] Prevent SPGBox from mutating xs0 · rht/climate\_stress\_test\_benchmark@caea4dd · GitHub](https://github.com/rht/climate_stress_test_benchmark/commit/caea4ddc445687bba7735702593aa0254b55a576).

---

_[View the full topic](https://discourse.julialang.org/t/optim-jl-vs-scipy-optimize-once-again/61661)._
