Your check_inequalities_efficient looks fine to me. Also note that it will return false from the moment it encounters one inequality which is not satisfied, whereas check_inequalities will first create a BitVector potentially full of falses, and then return false when encountering the first false within. I.e. the second version is more efficient not only because we get rid of the allocations, but also because we perform fewer comparisons.
I’m personally not too fond of the indexing y[2], so here’s an alternative using an iterator
check_inequalities_efficient_zip(x::Vector{Float64}, rectangle::RectangleSpace) = all(lb <= v <= ub for (lb, v, ub) in zip(rectangle.lower_bounds, x, rectangle.upper_bounds))
julia> RunBench()
BenchmarkTools.Trial: 10000 samples with 985 evaluations.
Range (min … max): 56.142 ns … 3.489 μs ┊ GC (min … max): 0.00% … 96.77%
Time (median): 57.056 ns ┊ GC (median): 0.00%
Time (mean ± σ): 65.460 ns ± 86.602 ns ┊ GC (mean ± σ): 8.44% ± 6.47%
█▅▁ ▃▁ ▁
████▇▆▇▇▇▄▆▄▄▅▅▃▄▃▄▄▃▆███▆▆▅▅▄▅▅▁▄▄▃▃▄▄▃▃▄▄▃▄▃▁▅▃▄▄▃▃▁▄▁▃▅▄ █
56.1 ns Histogram: log(frequency) by time 146 ns <
Memory estimate: 96 bytes, allocs estimate: 3.
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min … max): 4.300 ns … 65.200 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 4.300 ns ┊ GC (median): 0.00%
Time (mean ± σ): 4.426 ns ± 1.129 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
█
██▂▁▂▁▁▁▁▂▂▂▁▂▂▂▂▂▂▁▂▁▂▁▁▁▂▁▁▁▁▂▁▁▁▁▁▁▁▁▁▁▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▂ ▂
4.3 ns Histogram: frequency by time 9.2 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min … max): 2.600 ns … 13.800 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 2.700 ns ┊ GC (median): 0.00%
Time (mean ± σ): 2.800 ns ± 0.665 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
▃█▃ ▁
███▁▄▃▁▁▄▄▁▄▄▁▆▄▁▁▃▄▁▃▃▁▃▁▁▁▃▁▁▃▁▁▁▁▄▁▁▄▁▅▇▅▁▄▁▁▄▃▁▁▃▁▁▁▃▁ █
2.6 ns Histogram: log(frequency) by time 6.7 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
(check_inequalities, check_inequalities_efficient, check_inequalities_efficient_zip)