-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
filter
sometimes slower than going via map
#32303
Comments
Seems like you hit this: Lines 337 to 343 in f49cb42
At first, I thought that this method gets called, which is synonymous to your faster methods: Line 2312 in 55e36cc
But actually, it's this one: Line 2351 in 55e36cc
which leads to the first code quote. In this spirit, consider this: julia> haystack = reshape(rand(100_000), 1,100000)
1×100000 Array{Float64,2}:
0.420187 0.60593 0.512718 0.174329 … 0.657166 0.304862 0.223922
julia> @benchmark filter(x -> x < 0.5, $haystack)
BenchmarkTools.Trial:
memory estimate: 489.33 KiB
allocs estimate: 8
--------------
minimum time: 442.609 μs (0.00% GC)
median time: 461.997 μs (0.00% GC)
mean time: 513.938 μs (2.02% GC)
maximum time: 35.242 ms (98.61% GC)
--------------
samples: 9680
evals/sample: 1
julia> @benchmark $haystack[map(x -> x < 0.5, $haystack)]
BenchmarkTools.Trial:
memory estimate: 489.28 KiB
allocs estimate: 7
--------------
minimum time: 442.198 μs (0.00% GC)
median time: 462.534 μs (0.00% GC)
mean time: 517.566 μs (2.02% GC)
maximum time: 32.510 ms (98.60% GC)
--------------
samples: 9610
evals/sample: 1 Now you trigger the code that is synonymous to your faster method. |
Yes, must be due to using repeated |
Could |
I guess it depends on how often the boolean function returns julia> using BenchmarkTools
julia> haystack = rand(100_000);
julia> @benchmark filter(x -> x < 5e-5, $haystack)
BenchmarkTools.Trial:
memory estimate: 128 bytes
allocs estimate: 2
--------------
minimum time: 83.695 μs (0.00% GC)
median time: 83.765 μs (0.00% GC)
mean time: 91.617 μs (0.00% GC)
maximum time: 2.148 ms (0.00% GC)
--------------
samples: 10000
evals/sample: 1
julia> haystack = reshape(haystack, 1,100000);
julia> @benchmark filter(x -> x < 5e-5, $haystack)
BenchmarkTools.Trial:
memory estimate: 97.97 KiB
allocs estimate: 7
--------------
minimum time: 60.317 μs (0.00% GC)
median time: 101.090 μs (0.00% GC)
mean time: 124.740 μs (12.38% GC)
maximum time: 51.757 ms (99.66% GC)
--------------
samples: 10000
evals/sample: 1 So the |
I just realized that there is WIP in #31929 regarding this exact issue. So I assume we can close this? |
Yes, this can be closed when that PR is merged. Thanks! |
Which it now is. |
Here's are a couple different cases I found in the wild (I've not done extensive benchmarking). I found this result surprising.
Strings:
Numbers:
Version:
The text was updated successfully, but these errors were encountered: