4 likes
·
447 reads
3 comments
Regarding the first example it's interesting to note that while for small arrays and searching for a frequent occurence using x->xx<num seems directly related to the rarity of the searched for condition. Even for small arrays where the condition does not occur, this approah is significantly slower than the other approaches (and the vectorized approach is fastest).
Hi Wolf, not sure what you are testing. Note that the timing depends on how you test it.
The any(x -> x < 3, numbers)
might look slow if you use @time
, but that's because the anonymous function keeps getting recompiled for every call. Maybe that's what you mean?
You can easily bypass this by placing it inside another function. Also you can use BenchmarkTools.@btime
instead to ignore the compilation time.
julia> numbers = [1, 2, 3, 4, 5];
julia> @time any(x -> x < 3, numbers)
0.006801 seconds (2.74 k allocations: 183.680 KiB, 99.01% compilation time)
true
julia> @time any(numbers .< 3)
0.000012 seconds (3 allocations: 128 bytes)
true
julia> any_less(numbers, value) = any(x -> x < value, numbers)
any_less (generic function with 1 method)
julia> @time any_less(numbers, 3) # first call compiles the `any_less`
0.009657 seconds (6.90 k allocations: 456.047 KiB, 99.76% compilation time)
true
julia> @time any_less(numbers, 3) # now it's fast
0.000005 seconds
true
But perhaps you are talking about something else?
I had made a mistake, wrote my code as a script as such:
using BenchmarkTools
numbers = rand(100)
num = 0.001
println("Size of test array $(length(numbers))")
println("Roughly $(num*100)% of entries satisfy condition (entry < $num)")
println()
println("result with <($num)")
@btime any(<(num), numbers)
println("result with x -> x < $num")
@btime any(x -> x < num, numbers)
println("numbers .< $num")
@btime any(numbers .< num)
However this has to allocate space for the array and number for each call to any. In the any(x -> x < num)
case it even has to allocate num for each iteration done by any.
This lead to confusion on my part as any(x -> x < num)
slowed down more than the other approaches when more iterations were required to find a match which I initially did not expect.