jmejia8 / Metaheuristics.jl

High-performance metaheuristics for optimization coded purely in Julia.
https://jmejia8.github.io/Metaheuristics.jl/stable/
Other
255 stars 26 forks source link

remove reverse of bit constructions for 2x speed #95

Closed jbytecode closed 1 year ago

jbytecode commented 1 year ago

This PR replaces

reverse(map(i-> 43543523 >> i & 1, 0:31))

with

map(i-> 43543523 >> i & 1, 31:(-1):0)

and this makes it approximately 2x faster in the bit calculations of MCCGA.

jbytecode commented 1 year ago
julia> @benchmark reverse(map(i-> 43543523 >> i & 1, 0:31))
BenchmarkTools.Trial: 10000 samples with 973 evaluations.
 Range (min … max):  68.154 ns … 440.565 ns  ┊ GC (min … max): 0.00% … 62.67%
 Time  (median):     83.844 ns               ┊ GC (median):    0.00%
 Time  (mean ± σ):   94.819 ns ±  32.122 ns  ┊ GC (mean ± σ):  4.78% ± 10.19%

     ▇█▂                                                        
  ▁▃▆███▇▅▄▄▃▃▃▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁ ▂
  68.2 ns         Histogram: frequency by time          245 ns <

 Memory estimate: 672 bytes, allocs estimate: 2.

julia> @benchmark map(i-> 43543523 >> i & 1, 31:(-1):0)
BenchmarkTools.Trial: 10000 samples with 996 evaluations.
 Range (min … max):  28.897 ns …   4.360 μs  ┊ GC (min … max):  0.00% … 97.10%
 Time  (median):     93.365 ns               ┊ GC (median):     0.00%
 Time  (mean ± σ):   88.328 ns ± 246.935 ns  ┊ GC (mean ± σ):  16.53% ±  5.90%

   █▁                                                           
  ▄██▅▅▄▃▃▃▃▃▃▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▁▂▂▄▇▇█▅▄▅▄▄▃▄▄▃▃▃▃▃▃▂▂▂▂ ▃
  28.9 ns         Histogram: frequency by time          137 ns <

 Memory estimate: 336 bytes, allocs estimate: 1.
jmejia8 commented 1 year ago

Nice! Thank you!

jbytecode commented 1 year ago

@jmejia8 - thank you for merging!