Skip to content

maximum is 2/3 times slower than explicit for loop #26003

@piever

Description

@piever

Using maximum with a function as first argument (or with a generator) can be 2 or 3 times slower than an explicit for loop, where intuitively they should take more or less the same time: see this comment on discourse. Here I'm computing the maximum of the absolute value of elements in an array without allocating the intermediate abs.(v) array.

julia> v = randn(1000000);

julia> function maxabs(v)
       m = abs(v[1])
       for i in 2:length(v)
       z = abs(v[i])
       z>m && (m=z)
       end
       m
       end
maxabs (generic function with 1 method)

julia> @benchmark maxabs($v)
BenchmarkTools.Trial: 
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     594.965 μs (0.00% GC)
  median time:      622.898 μs (0.00% GC)
  mean time:        633.162 μs (0.00% GC)
  maximum time:     1.057 ms (0.00% GC)
  --------------
  samples:          7867
  evals/sample:     1

julia> @benchmark maximum(abs, $v)
BenchmarkTools.Trial: 
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     1.586 ms (0.00% GC)
  median time:      1.659 ms (0.00% GC)
  mean time:        1.682 ms (0.00% GC)
  maximum time:     2.718 ms (0.00% GC)
  --------------
  samples:          2968
  evals/sample:     1

julia> @benchmark maximum(abs(el) for el in $v)
BenchmarkTools.Trial: 
  memory estimate:  16 bytes
  allocs estimate:  1
  --------------
  minimum time:     1.482 ms (0.00% GC)
  median time:      1.504 ms (0.00% GC)
  mean time:        1.538 ms (0.00% GC)
  maximum time:     2.682 ms (0.00% GC)
  --------------
  samples:          3245
  evals/sample:     1

I'm on Julia 0.7, but the same issue appeared on Julia 0.6 (I have to note that the generator version improved between 0.6 and 0.7: in 0.6 it was slower than the function version, now they are comparable).

julia> versioninfo()
Julia Version 0.7.0-DEV.3634
Commit 8d8f960faa (2018-01-29 10:32 UTC)
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, skylake)
Environment:

EDIT: the issue is about maximum, not about generators, see comments below. I've edited the title accordingly.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions