Skip to content

Reduction on array view is slow #28848

@ZeppLu

Description

@ZeppLu
julia> using BenchmarkTools; const X = rand(1000, 1000);

julia> sum(X); @benchmark sum(X)
BenchmarkTools.Trial: 
  memory estimate:  0 bytes
  allocs estimate:  0
  --------------
  minimum time:     355.773 μs (0.00% GC)
  median time:      406.994 μs (0.00% GC)
  mean time:        453.882 μs (0.00% GC)
  maximum time:     2.171 ms (0.00% GC)
  --------------
  samples:          10000
  evals/sample:     1

julia> sum(@view X[1:999, 1:999]); @benchmark sum(@view X[1:999, 1:999])
BenchmarkTools.Trial: 
  memory estimate:  64 bytes
  allocs estimate:  1
  --------------
  minimum time:     1.782 ms (0.00% GC)
  median time:      1.795 ms (0.00% GC)
  mean time:        1.815 ms (0.00% GC)
  maximum time:     2.616 ms (0.00% GC)
  --------------
  samples:          2750
  evals/sample:     1

julia> versioninfo()
Julia Version 1.0.0
Commit 5d4eaca0c9 (2018-08-08 20:58 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-6.0.0 (ORCJIT, skylake)

Compared to python:

In [1]: import numpy as np; X = np.random.random([1000, 1000])

In [2]: %timeit np.sum(X)
377 µs ± 5.1 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

In [3]: %timeit np.sum(X[:999, :999])
752 µs ± 21.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

This issue holds for prod() as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    arrays[a, r, r, a, y, s]foldsum, maximum, reduce, foldl, etc.performanceMust go faster

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions