bankofcanada / ModelBaseEcon.jl

BSD 3-Clause "New" or "Revised" License
26 stars 5 forks source link

Precompute looking up indices for all variables in `update_auxvars` #41

Closed KristofferC closed 1 year ago

KristofferC commented 1 year ago

Looking up the index for a single variable is quite expensive since we need to find that variable in a vector. The current method ended up calling indexin which is not optimized well for finding a single value since it does quite a bit of expensive stuff under the hood (like creating a dictionary).

Instead, in this PR, we precompute a variable => index mapping for all variables which is done via a simple loop and then use that mapping when an index is needed.

This takes the time for solving the FRB-US model (following the tutorial in TutorialsEcon and using the @btime macro from BenchmarkTools.jl) from

julia> @btime simulate(m, p_0, ed_0; verbose=true, tol=1e-12);
  472.368 ms (1646933 allocations: 3.80 GiB)

to

julia> @btime simulate(m, p_0, ed_0; verbose=true, tol=1e-12);
  148.714 ms (1389881 allocations: 946.72 MiB)
codecov-commenter commented 1 year ago

Codecov Report

Merging #41 (fff9099) into master (7a7dfa8) will increase coverage by 0.01%. The diff coverage is 100.00%.

:mega: This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@            Coverage Diff             @@
##           master      #41      +/-   ##
==========================================
+ Coverage   94.43%   94.44%   +0.01%     
==========================================
  Files          15       15              
  Lines        1671     1675       +4     
==========================================
+ Hits         1578     1582       +4     
  Misses         93       93              
Impacted Files Coverage Δ
src/model.jl 96.65% <100.00%> (+0.02%) :arrow_up:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.