# How to efficiently create a table/array through known function?

**URL:** <https://discourse.julialang.org/t/how-to-efficiently-create-a-table-array-through-known-function/88965>\
**Category:** Performance\
**Tags:** arrays, staticarrays\
**Created:** [October 19, 2022, 1:31pm UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-table-array-through-known-function/88965 "2022-10-19T13:31:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![swish47](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swish47/32/43921_2.png) [@swish47](https://discourse.julialang.org/u/swish47)\
**Post date:** [October 19, 2022, 1:31pm UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-table-array-through-known-function/88965/1 "2022-10-19T13:31:00Z")

</div>

As the question, if I want to create a table by a function, I may use collect:

```julia
@elapsed collect(Vector{Float64}(m*[2*pi,(2pi)/sqrt(3)]+n*[0,(4*pi)/sqrt(3)]) for m in range(0.,1.,1001), n in range(0.,1.,1001))

```

with output 0.9724441 in my laptop.  
However, I could directly use Table in Mathematica:

```julia
N[Table[Evaluate[
    N[m*{2 \[Pi], (2 \[Pi])/Sqrt[3]} + n*{0, (4 \[Pi])/Sqrt[3]}]], 
   Evaluate[
    Sequence @@ 
     N[{{m, 0, 1, 1/1001}, {n, 0, 1, 1/1001}}]]]] // AbsoluteTiming

```

with only 0.08 seconds. Therefore, could I try some more efficient method to generate an list?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 19, 2022, 1:42pm UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-table-array-through-known-function/88965/2 "2022-10-19T13:42:40Z")

</div>

> [@swish47](#):
>
> ```julia
> @elapsed collect(Vector{Float64}(m*[2*pi,(2pi)/sqrt(3)]+n*[0,(4*pi)/sqrt(3)]) for m in range(0.,1.,1001), n in range(0.,1.,1001))
> 
> ```

What you are creating here is a matrix of vectors. Is that what you want?

If it is that, and the vectors of the matrix have actually only 2 elements, you can create them with `StaticArrays`, by replacing the `[]` constructors by `SVector()`:

```julia
julia> using StaticArrays

julia> @elapsed collect((m*SVector(2*pi,(2pi)/sqrt(3))+n*SVector(0,(4*pi)/sqrt(3))) for m in range(0.,1.,1001), n in range(0.,1.,1001))
0.032893729

```

Nevertheless, I find likely that that is not exactly what you want for an output (I cannot test what the Mathematica code produces).

---

<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:** [October 19, 2022, 1:43pm UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-table-array-through-known-function/88965/3 "2022-10-19T13:43:10Z")

</div>

Because each matrix entry is a two-element Vector, you’ll have one newly allocated vector times the number of elements in your matrix, the multiplications and addition you’re doing even increases the allocations. Instead you can work with tuples, for example:

```julia
function func()
    [m .* (2*pi,(2pi)/sqrt(3)) .+ n .* (0.0,(4*pi)/sqrt(3)) for m in range(0.,1.,1001), n in range(0.,1.,1001)]
end

@time func()
0.005967 seconds (2 allocations: 15.289 MiB)

```

Edit: A tad slower than @lmiq 🙂

---

<div class="post-metadata">

**Author:** ![swish47](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swish47/32/43921_2.png) [@swish47](https://discourse.julialang.org/u/swish47)\
**Post date:** [October 19, 2022, 2:56pm UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-table-array-through-known-function/88965/4 "2022-10-19T14:56:03Z")

</div>

Thanks for your reply. `SVector ` is an extremely effective function, which can solve my problem.

---

<div class="post-metadata">

**Author:** ![swish47](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swish47/32/43921_2.png) [@swish47](https://discourse.julialang.org/u/swish47)\
**Post date:** [October 19, 2022, 6:21pm UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-table-array-through-known-function/88965/5 "2022-10-19T18:21:49Z")

</div>

Thanks for your effectual suggestion. Could I know what is the mechanism of `[space].* `? It seems I couldn’t directly multiply a number with an tuple?

---

<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:** [October 19, 2022, 6:23pm UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-table-array-through-known-function/88965/6 "2022-10-19T18:23:45Z")

</div>

It’s broadcasting, or syntactic loop fusion:

> **[More Dots: Syntactic Loop Fusion in Julia](https://julialang.org/blog/2017/01/moredots/)**
>
> More Dots: Syntactic Loop Fusion in Julia | After a lengthy design process (https://github.com/JuliaLang/julia/issues/8450) and preliminary foundations in Julia 0.5 (/blog/2016-10-11-julia-0.5-highlights#vectorized\_function\_calls), Julia 0.6 includes...

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [October 19, 2022, 6:43pm UTC](https://discourse.julialang.org/t/how-to-efficiently-create-a-table-array-through-known-function/88965/7 "2022-10-19T18:43:33Z")

</div>

> [@swish47](#):
>
> It seems I couldn’t directly multiply a number with an tuple?

That is why I suggested the `SVector`. These static vectors are just tuples as well, but for which all arithmetic is defined. If that is important or not it depends on what you are doing with the vectors afterwards.
