# Array confusion

**URL:** https://discourse.julialang.org/t/array-confusion/29516
**Category:** New to Julia
**Created:** [October 5, 2019, 6:02am UTC](https://discourse.julialang.org/t/array-confusion/29516 "2019-10-05T06:02:27Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![jonoke](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@jonoke](https://discourse.julialang.org/u/jonoke)
#### Post date: [October 5, 2019, 6:02am UTC](https://discourse.julialang.org/t/array-confusion/29516/1 "2019-10-05T06:02:27Z")

</div>

Could someone explain this idiom please… I don’t know what is going on, despite seeing that the result makes sense.

julia\> [5 for i in 1:3]  
3-element Array{Int64,1}:  
5  
5  
5

Thanks.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 5, 2019, 6:28am UTC](https://discourse.julialang.org/t/array-confusion/29516/2 "2019-10-05T06:28:23Z")

</div>

[https://docs.julialang.org/en/latest/manual/arrays/#Comprehensions-1](https://docs.julialang.org/en/latest/manual/arrays/#Comprehensions-1)

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [October 5, 2019, 12:43pm UTC](https://discourse.julialang.org/t/array-confusion/29516/3 "2019-10-05T12:43:57Z")

</div>

AFAIK this is just a shorthand for a `for` loop:

```julia
x = Vector{Float64}(undef, 3)
for j in 1 : 3
  x[j] = 5
end

```

Whether there is a performance benefit is not clear to me.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 5, 2019, 12:59pm UTC](https://discourse.julialang.org/t/array-confusion/29516/4 "2019-10-05T12:59:16Z")

</div>

> [@hendri54](#):
>
> AFAIK this is just a shorthand for a `for` loop:

Not really, it lowers into a `collect` call.

> [@hendri54](#):
>
> Whether there is a performance benefit is not clear to me.

Besides the obvious syntactic advantages (it is shorter), you don’t have to calculate a container element type, and can also filter on the fly.

---

<div class="post-metadata">

### Author: ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)
#### Post date: [October 5, 2019, 2:20pm UTC](https://discourse.julialang.org/t/array-confusion/29516/5 "2019-10-05T14:20:16Z")

</div>

So I should expect the comprehension to be generically faster than the loop?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [October 5, 2019, 2:52pm UTC](https://discourse.julialang.org/t/array-confusion/29516/6 "2019-10-05T14:52:16Z")

</div>

No (and I did not say this — please read what I said). It is not about speed _per se_.

When the compiler can infer types, you know the number of elements in advance, and you take the allocation of the result container into account, I would not expect a difference in speed.
