InvertedIndices.jl icon indicating copy to clipboard operation
InvertedIndices.jl copied to clipboard

Significant performance cost over naively filtering indices

Open ararslan opened this issue 5 years ago • 0 comments

I went to port Jackknife over to use InvertedIndices, but I noticed something... suboptimal.

Below are some timings for using InvertedIndices (thing1) and using the current naive approach for skipping indices (thing2).

julia> using InvertedIndices, Statistics, BenchmarkTools

julia> thing1(f, x) = map(i->f(view(x, Not(i))), eachindex(x))
thing1 (generic function with 1 method)

julia> thing2(f, x) = (is = eachindex(x); map(i->f(view(x, filter(!isequal(i), is))), is))
thing2 (generic function with 1 method)

julia> x = randn(2000);

julia> @benchmark thing1(mean, $x)
BenchmarkTools.Trial:
  memory estimate:  792.91 MiB
  allocs estimate:  27954027
  --------------
  minimum time:     2.523 s (1.98% GC)
  median time:      2.564 s (2.81% GC)
  mean time:        2.564 s (2.81% GC)
  maximum time:     2.605 s (3.61% GC)
  --------------
  samples:          2
  evals/sample:     1

julia> @benchmark thing2(mean, $x)
BenchmarkTools.Trial:
  memory estimate:  35.48 MiB
  allocs estimate:  28005
  --------------
  minimum time:     18.324 ms (3.89% GC)
  median time:      20.482 ms (10.97% GC)
  mean time:        20.770 ms (11.10% GC)
  maximum time:     64.851 ms (64.83% GC)
  --------------
  samples:          241
  evals/sample:     1

That's pretty hardcore. Am I doing something wrong in how I'm using it here?

ararslan avatar May 05 '19 04:05 ararslan