I don’t understand why the actual find
function fills a 0 length array using push!
to finally create other array and copy data to it. However, I write this version which avoid calling push!
and it’s faster than the actual implementation. Should I make a PR with this implementation? Best,
julia> using BenchmarkTools
julia> function find2(testf::Function, A)
len = 0
inds = Base._index_remapper(A)
out = Array(Int, length(inds))
for (i,a) = enumerate(A)
if testf(a)
len += 1
out[len] = inds[i]
end
end
resize!(out, len)
return I
end
find2 (generic function with 1 method)
julia> @benchmark find(x -> x > 0.5, rand(10000))
BenchmarkTools.Trial:
memory estimate: 244.61 kb
allocs estimate: 17
--------------
minimum time: 130.360 μs (0.00% GC)
median time: 143.276 μs (0.00% GC)
mean time: 152.181 μs (3.08% GC)
maximum time: 694.662 μs (76.48% GC)
--------------
samples: 10000
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%
julia> @benchmark find2(x -> x > 0.5, rand(10000))
BenchmarkTools.Trial:
memory estimate: 156.41 kb
allocs estimate: 4
--------------
minimum time: 79.431 μs (0.00% GC)
median time: 83.703 μs (0.00% GC)
mean time: 89.274 μs (2.98% GC)
maximum time: 558.110 μs (72.61% GC)
--------------
samples: 10000
evals/sample: 1
time tolerance: 5.00%
memory tolerance: 1.00%