# Why is there a massive difference between collect and \[range...\]?

**URL:** <https://discourse.julialang.org/t/why-is-there-a-massive-difference-between-collect-and-range/90497>\
**Category:** New to Julia\
**Tags:** performance\
**Created:** [November 19, 2022, 1:38pm UTC](https://discourse.julialang.org/t/why-is-there-a-massive-difference-between-collect-and-range/90497 "2022-11-19T13:38:30Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![AwesomeQuest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/awesomequest/32/38910_2.png) [@AwesomeQuest](https://discourse.julialang.org/u/AwesomeQuest)\
**Post date:** [November 19, 2022, 1:38pm UTC](https://discourse.julialang.org/t/why-is-there-a-massive-difference-between-collect-and-range/90497/1 "2022-11-19T13:38:30Z")

</div>

```julia
julia> @time k = collect(1:10^7)
  0.294210 seconds (2 allocations: 76.294 MiB, 90.43% gc time)
10000000-element Vector{Int64}:
        1
        2
        ⋮
  9999999
 10000000

julia> @time k = [(1:10^7)...]
  1.663185 seconds (30.00 M allocations: 925.627 MiB, 28.68% gc time)
10000000-element Vector{Int64}:
        1
        2
        ⋮
  9999999
 10000000

```

Why is there such a big difference? Is splat just inefficient?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 19, 2022, 1:43pm UTC](https://discourse.julialang.org/t/why-is-there-a-massive-difference-between-collect-and-range/90497/2 "2022-11-19T13:43:37Z")

</div>

The second one is a function call with 10^7 arguments while the first one is a function call with one argument. I don’t know exactly how the compiler even deals with the second case but it’s intuitively clear that it’s more difficult to optimize, no?

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [November 19, 2022, 2:07pm UTC](https://discourse.julialang.org/t/why-is-there-a-massive-difference-between-collect-and-range/90497/3 "2022-11-19T14:07:05Z")

</div>

To elaborate on the function call comment: `[x...]` seems to translate to `vect(x...)` after de-sugaring.

Just an idle observation: If you want to make your introspection tools hang/crash try

```julia
julia> x = (1:10^7)
1:10000000

julia> @code_warntype [x...] # very unhappy trying to list 10^7 arguments

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [November 19, 2022, 2:33pm UTC](https://discourse.julialang.org/t/why-is-there-a-massive-difference-between-collect-and-range/90497/4 "2022-11-19T14:33:53Z")

</div>

> [@jules](#):
>
> I don’t know exactly how the compiler even deals with the second case but it’s intuitively clear that it’s more difficult to optimize, no?

The general rule of thumb for splatting is not to use it if you don’t know the length of the thing you’re splatting at compile time/the compiler doesn’t know it (as is the case for `Vector`). That’s the reason why we have both `min` and `minimum`, `max` and `maximum` and so on.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [November 19, 2022, 3:42pm UTC](https://discourse.julialang.org/t/why-is-there-a-massive-difference-between-collect-and-range/90497/5 "2022-11-19T15:42:41Z")

</div>

I assume the compiler can still kind of deal with all those arguments because it knows they’re of the same type. Like in Vararg{Int}. But I assume if that pattern was broken (let’s say Float64 and Int interleaved for 10e7 arguments, it would be worse.

---

<div class="post-metadata">

**Author:** ![jishnub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jishnub/32/33620_2.png) [@jishnub](https://discourse.julialang.org/u/jishnub)\
**Post date:** [November 19, 2022, 4:41pm UTC](https://discourse.julialang.org/t/why-is-there-a-massive-difference-between-collect-and-range/90497/6 "2022-11-19T16:41:16Z")

</div>

What if you know at compile time that the length is 10^7? Suppose it’s a static range. Wouldn’t it still be a bad idea to try to compile a method with that many arguments?

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [November 19, 2022, 11:35pm UTC](https://discourse.julialang.org/t/why-is-there-a-massive-difference-between-collect-and-range/90497/7 "2022-11-19T23:35:17Z")

</div>

> [@jules](#):
>
> I assume the compiler can still kind of deal with all those arguments because it knows they’re of the same type. Like in Vararg{Int}. But I assume if that pattern was broken (let’s say Float64 and Int interleaved for 10e7 arguments, it would be worse.

No, them being the same type doesn’t help at all.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [November 19, 2022, 11:35pm UTC](https://discourse.julialang.org/t/why-is-there-a-massive-difference-between-collect-and-range/90497/8 "2022-11-19T23:35:38Z")

</div>

> [@jishnub](#):
>
> What if you know at compile time that the length is 10^7? Suppose it’s a static range. Wouldn’t it still be a bad idea to try to compile a method with that many arguments?

Yes, it would still be a bad idea.
