# Sum over LazyArray is slower than over regular array

**URL:** https://discourse.julialang.org/t/sum-over-lazyarray-is-slower-than-over-regular-array/136989
**Category:** Performance
**Tags:** question, package, lazy-evaluation
**Created:** [May 5, 2026, 2:04pm UTC](https://discourse.julialang.org/t/sum-over-lazyarray-is-slower-than-over-regular-array/136989 "2026-05-05T14:04:13Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![jecs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jecs/32/1781_2.png) [@jecs](https://discourse.julialang.org/u/jecs)
#### Post date: [May 5, 2026, 2:04pm UTC](https://discourse.julialang.org/t/sum-over-lazyarray-is-slower-than-over-regular-array/136989/1 "2026-05-05T14:04:13Z")

</div>

```julia-auto
julia> N = 64; A = randn(N,N); B = randn(N,N); C = similar(A,1,N);

```

Using Lazy Arrays:

```julia-auto
julia> @btime sum!($C,LazyArray(@~ $A.*$B))
  3.047 μs (0 allocations: 0 bytes)

```

Without LazyArrays:

```julia-auto
julia> @btime sum!($C,$A.*$B)
  1.163 μs (3 allocations: 32.08 KiB)

```

Even with allocations, regular `sum!` is about 3x faster on my machine. Is this expected behavior?

Interestingly, using `map!` with `eachcol` outperforms both approaches:

```julia-auto
julia> using LinearAlgebra

julia> @btime map!(⋅,$C,eachcol($A),eachcol($B))
  788.132 ns (0 allocations: 0 bytes)

```

Here’s the output of `versioninfo`:

```julia-auto
julia> versioninfo()
Julia Version 1.12.6
Commit 15346901f00 (2026-04-09 19:20 UTC)
Build Info:
  Official https://julialang.org release
Platform Info:
  OS: macOS (arm64-apple-darwin24.0.0)
  CPU: 8 × Apple M2
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, apple-m2)
  GC: Built with stock GC
Threads: 4 default, 1 interactive, 4 GC (on 4 virtual cores)
Environment:
  JULIA_EDITOR = code
  JULIA_VSCODE_REPL = 1
  JULIA_PKG_USE_CLI_GIT = true

```

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [May 5, 2026, 2:10pm UTC](https://discourse.julialang.org/t/sum-over-lazyarray-is-slower-than-over-regular-array/136989/2 "2026-05-05T14:10:44Z")

</div>

it’s not at all surprising that `Array` outperforms `LazyArray`. something like `sum(::Array)` is a very highly-trafficked method and thus has had a lot of attention for optimization. the nature of `LazyArray` means it’s probably going to have to do more work on access, might not be able to vectorize as well, etc.

I’m not sure about your `map!` with `eachcol` form. if I had to guess, it’s because `LinearAlgebra.dot` gets to use BLAS while `Base.sum` is all-Julia

---

<div class="post-metadata">

### Author: ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)
#### Post date: [May 5, 2026, 2:31pm UTC](https://discourse.julialang.org/t/sum-over-lazyarray-is-slower-than-over-regular-array/136989/3 "2026-05-05T14:31:02Z")

</div>

`Tullio.jl` is 2x faster

```julia-auto
julia> using Tullio

julia> @btime @tullio $C[1, j] = $A[i, j] * $B[i, j]
  622.337 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

### Author: ![jecs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jecs/32/1781_2.png) [@jecs](https://discourse.julialang.org/u/jecs)
#### Post date: [May 5, 2026, 2:32pm UTC](https://discourse.julialang.org/t/sum-over-lazyarray-is-slower-than-over-regular-array/136989/4 "2026-05-05T14:32:56Z")

</div>

Wow! I’ll check that package out. Those are impressive numbers.
