Skip to content
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

Performance issue with enumerate #22897

Closed
itsdfish opened this issue Jul 21, 2017 · 1 comment
Closed

Performance issue with enumerate #22897

itsdfish opened this issue Jul 21, 2017 · 1 comment

Comments

@itsdfish
Copy link

I found a performance issue with enumerate. Here is my version information:

Julia Version 0.6.0 Commit 9036443 (2017-06-19 13:05 UTC) Platform Info: OS: Linux (x86_64-pc-linux-gnu) CPU: Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz WORD_SIZE: 64 BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell) LAPACK: libopenblas64_ LIBM: libopenlibm LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

The following code compares the execution time of enumerate and an iterator with a manually defined index. As you will see, enumerate is about 4-5 times slower.

using BenchmarkTools

type Person
  id::Int
end

function getID1(people)
  ids = fill(0,size(people,1))
  for (i,p) in enumerate(people)
    ids[i] = p.id
  end
  return ids
end

function getID2(people)
  ids = fill(0,size(people,1))
  i = 0
  for p in people
    i += 1
    ids[i] = p.id
  end
  return ids
end

people = [Person(id) for id in 1:1000]

v1 = @benchmarkable getID1(people)
tune!(v1)
r1 = run(v1)

v2 = @benchmarkable getID2(people)
tune!(v2)
r2 = run(v2)

Here are the results for enumerate:

BenchmarkTools.Trial: 
  memory estimate:  39.19 KiB
  allocs estimate:  1001
  --------------
  minimum time:     6.356 μs (0.00% GC)
  median time:      7.906 μs (0.00% GC)
  mean time:        11.842 μs (25.96% GC)
  maximum time:     529.366 μs (94.82% GC)
  --------------
  samples:          10000
  evals/sample:     5

And the results for the corresponding manual indexing:

BenchmarkTools.Trial: 
  memory estimate:  7.94 KiB
  allocs estimate:  1
  --------------
  minimum time:     1.886 μs (0.00% GC)
  median time:      2.088 μs (0.00% GC)
  mean time:        2.550 μs (10.38% GC)
  maximum time:     140.503 μs (87.76% GC)
  --------------
  samples:          10000
  evals/sample:     10

@KristofferC
Copy link
Member

Dup of #16190 (which is dup of referenced issues therein).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants