# Small inconsistency between generators and vectors

**URL:** https://discourse.julialang.org/t/small-inconsistency-between-generators-and-vectors/79950
**Category:** New to Julia
**Created:** [April 24, 2022, 6:15pm UTC](https://discourse.julialang.org/t/small-inconsistency-between-generators-and-vectors/79950 "2022-04-24T18:15:06Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![goerch](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerch/32/29122_2.png) [@goerch](https://discourse.julialang.org/u/goerch)
#### Post date: [April 24, 2022, 6:15pm UTC](https://discourse.julialang.org/t/small-inconsistency-between-generators-and-vectors/79950/1 "2022-04-24T18:15:06Z")

</div>

Just noticed, the MWE

```julia
using Random
using BenchmarkTools
Random.seed!(42)
array = rand(1_000_000)
x = @btime sum([y*y for y in $array]) 
z = @btime sum(y*y for y in $array) 
@show x
@show z
@assert z == x

```

yields (obviously?)

```julia
  1.810 ms (2 allocations: 7.63 MiB)
  1.006 ms (0 allocations: 0 bytes)
x = 332909.22931042255
z = 332909.2293104364
ERROR: AssertionError: z == x

```

so Julia doesn’t always use pairwise sum?

Edit: noticed this analyzing @TouchSir’s [posting](https://discourse.julialang.org/t/jit-compilation-does-not-avoid-useless-computations/79939/7).

---

<div class="post-metadata">

### Author: ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)
#### Post date: [April 24, 2022, 6:22pm UTC](https://discourse.julialang.org/t/small-inconsistency-between-generators-and-vectors/79950/2 "2022-04-24T18:22:26Z")

</div>

> [@goerch](#):
>
> so Julia doesn’t always use pairwise sum?

It isn’t possible for the generators (second) version. The generator can only be traversed linearly.  
Well, in your example it is possible, as the array is already realized, but this special case is probably not considered in the code. Your example is a bit artificial, why would you create a generator over an existing array?
