JuliaDynamics / Agents.jl

Agent-based modeling framework in Julia
https://juliadynamics.github.io/Agents.jl/stable/
MIT License
717 stars 115 forks source link

Faster reservoir sampling #913

Closed Tortar closed 9 months ago

Tortar commented 9 months ago

This is faster, as some benchmark show, same code and metodology of #788

before

julia> @benchmark random_nearby_position($(50,50), $model, $1)
BenchmarkTools.Trial: 10000 samples with 958 evaluations.
 Range (min … max):   94.749 ns …  1.410 μs  ┊ GC (min … max): 0.00% … 90.62%
 Time  (median):     100.439 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   102.630 ns ± 32.078 ns  ┊ GC (mean ± σ):  0.74% ±  2.21%

             ▁▄▇▇██▆▃▁                                          
  ▁▁▁▁▁▁▂▂▃▅▇█████████▇▅▄▃▃▃▃▂▂▂▁▁▁▁▁▁▁▁▁▁▁▂▂▂▂▂▂▂▃▃▂▂▂▂▂▂▂▁▂▁ ▃
  94.7 ns         Histogram: frequency by time          115 ns <

 Memory estimate: 32 bytes, allocs estimate: 1.

julia> @benchmark random_nearby_position($(50,50), $model, $10)
BenchmarkTools.Trial: 10000 samples with 182 evaluations.
 Range (min … max):  594.137 ns … 823.137 ns  ┊ GC (min … max): 0.00% … 0.00%
 Time  (median):     639.610 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   640.772 ns ±  18.826 ns  ┊ GC (mean ± σ):  0.00% ± 0.00%

                 ▃▃▄▆▅▆▆▅▆▇▆█▇▆▆▆▆▅▅▃▃▁▁                         
  ▁▁▁▁▂▂▂▃▃▄▅▇▇███████████████████████████▇▆▅▄▄▃▃▃▂▂▂▂▂▂▂▁▂▁▁▁▁ ▅
  594 ns           Histogram: frequency by time          698 ns <

 Memory estimate: 32 bytes, allocs estimate: 1.

after

julia> @benchmark random_nearby_position($(50,50), $model, $1)
BenchmarkTools.Trial: 10000 samples with 962 evaluations.
 Range (min … max):  89.899 ns …  2.274 μs  ┊ GC (min … max): 0.00% … 93.24%
 Time  (median):     96.220 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   98.950 ns ± 34.391 ns  ┊ GC (mean ± σ):  0.79% ±  2.22%

           ▂▅███▄▁                                             
  ▂▂▁▂▂▂▃▅▇████████▆▅▄▄▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▃▃▃▃▃▃▃▃▃▃▃▃▃▃▃▂▂▂▂▂▂▂ ▃
  89.9 ns         Histogram: frequency by time         118 ns <

 Memory estimate: 32 bytes, allocs estimate: 1.

julia> @benchmark random_nearby_position($(50,50), $model, $10)
BenchmarkTools.Trial: 10000 samples with 359 evaluations.
 Range (min … max):  265.178 ns …  3.143 μs  ┊ GC (min … max): 0.00% … 87.86%
 Time  (median):     286.585 ns              ┊ GC (median):    0.00%
 Time  (mean ± σ):   288.582 ns ± 40.748 ns  ┊ GC (mean ± σ):  0.19% ±  1.24%

                 ▁▁▃▃▆▇█▇▇▇▇▇▅▆▅▂▃▃▂▃▁▁▁ ▁▁                     
  ▂▁▁▂▂▂▂▂▃▃▃▄▅▆▇█████████████████████████████▆▆▅▅▅▄▄▃▃▃▃▃▃▃▂▃ ▅
  265 ns          Histogram: frequency by time          312 ns <

 Memory estimate: 32 bytes, allocs estimate: 1.

This is based on the relationship between an exponential rv and the logarithm of a uniform rv

codecov-commenter commented 9 months ago

Codecov Report

Merging #913 (98f113e) into main (ea3aa78) will decrease coverage by 11.41%. The diff coverage is 100.00%.

@@             Coverage Diff             @@
##             main     #913       +/-   ##
===========================================
- Coverage   92.18%   80.77%   -11.41%     
===========================================
  Files          33       45       +12     
  Lines        2277     2955      +678     
===========================================
+ Hits         2099     2387      +288     
- Misses        178      568      +390     
Files Coverage Δ
src/core/space_interaction_API.jl 93.23% <100.00%> (+0.05%) :arrow_up:

... and 12 files with indirect coverage changes

:mega: We’re building smart automated test selection to slash your CI/CD build times. Learn more

Tortar commented 9 months ago

Apparently this PR reduces code coverage by 10% :D nah that's clearly a mistake.

Yes this a bit absurd ahah I noted that already on previous commits, it seems like codecov sometimes randomly doesn't understand what are the files it shouldn't cover

You mentioned that you wanted to make this into a package. I think it is a great idea. You should, it is definitely helpful for a wide range of applications. But it isn't a priority either!

Working on it at the moment https://github.com/Tortar/IteratorSampling.jl but I'm thinking about some good tricks to make everything better and the package is still not good enough to release 0.1 so I need some more time to finish up the work since uni work is coming for me :D