# Should I use broadcasting or write the function directly for higher dimensional input?

**URL:** <https://discourse.julialang.org/t/should-i-use-broadcasting-or-write-the-function-directly-for-higher-dimensional-input/91861>\
**Category:** New to Julia\
**Tags:** question, broadcasting\
**Created:** [December 19, 2022, 3:52pm UTC](https://discourse.julialang.org/t/should-i-use-broadcasting-or-write-the-function-directly-for-higher-dimensional-input/91861 "2022-12-19T15:52:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lighfe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lighfe/32/38831_2.png) [@Lighfe](https://discourse.julialang.org/u/Lighfe)\
**Post date:** [December 19, 2022, 3:52pm UTC](https://discourse.julialang.org/t/should-i-use-broadcasting-or-write-the-function-directly-for-higher-dimensional-input/91861/1 "2022-12-19T15:52:04Z")

</div>

Let’s say I have a 1000×3 Matrix{Int64}. I want to loop over each row and execute some function that doesn’t return anything.  
My intuitive approach was to write a function that takes a 3-element vector and later broadcast that function using mapslices.

But I think I might be a bit tunneling on this broadcasting thing.

Would it be less performant to write a for-loop that does the same thing and loops over every row?  
Or are there other best practices to loop over an input?

Would it be a different approach, if I want to actually map my input data?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 19, 2022, 4:10pm UTC](https://discourse.julialang.org/t/should-i-use-broadcasting-or-write-the-function-directly-for-higher-dimensional-input/91861/2 "2022-12-19T16:10:06Z")

</div>

> [@Lighfe](#):
>
> Would it be less performant to write a for-loop that does the same thing and loops over every row?

No, it would probably be _more_ performant to write a `for` loop, because broadcasting (or `map`) allocates an array for the result. If you don’t want to return anything, presumably you are doing something else, e.g. summing or something?

Loops are fast in Julia (if they are [written properly](https://docs.julialang.org/en/v1/manual/performance-tips/)). There’s nothing magical about broadcast operations except for the compact syntax — under the hood, they are implemented in Julia code with ordinary loops.

> Let’s say I have a 1000×3 Matrix{Int64}.

Note that if you are working with a bunch of 3-component vectors, e.g. for 3d coordinates, you should [consider using StaticArrays](https://docs.julialang.org/en/v1/manual/performance-tips/#Consider-StaticArrays.jl-for-small-fixed-size-vector/matrix-operations).

Note also that Julia arrays [are stored column-major](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-column-major) so accessing a 1000x3 array by rows will be less efficient than accessing a 3x1000 array by columns. (Or a length-1000 array of 3-component staticarrays.)

---

<div class="post-metadata">

**Author:** ![Lighfe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lighfe/32/38831_2.png) [@Lighfe](https://discourse.julialang.org/u/Lighfe)\
**Post date:** [December 19, 2022, 4:25pm UTC](https://discourse.julialang.org/t/should-i-use-broadcasting-or-write-the-function-directly-for-higher-dimensional-input/91861/3 "2022-12-19T16:25:06Z")

</div>

Thank you for the detailed answer!  
Yeah makes completely sense, so broadcasting should only be used, if I want to do some manipulations and return the result array? Or would I use a loop there as well?

I will definitely try out StaticArrays, too. And will start to access column wise. 👌
