# Memory allocations in for loop variable

**URL:** https://discourse.julialang.org/t/memory-allocations-in-for-loop-variable/14332
**Category:** Performance
**Tags:** question
**Created:** [August 30, 2018, 8:05pm UTC](https://discourse.julialang.org/t/memory-allocations-in-for-loop-variable/14332 "2018-08-30T20:05:43Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [August 30, 2018, 8:19pm UTC](https://discourse.julialang.org/t/memory-allocations-in-for-loop-variable/14332/2 "2018-08-30T20:19:31Z")

</div>

This code is an _excellent_ use case for StaticArrays.

Currently, you’re using a 2D matrix to store a list of 3-element points. That’s a very common idiom in Matlab and Numpy, but Julia can do better. If you instead store your vector of points as a `Vector` of 3-element static vectors, then it becomes easy to manipulate each element without allocating any memory at all.

Benchmarking your code:

```julia
julia> using BenchmarkTools

julia> @btime get_orientations!($rotvec, $ori)
  12.987 ms (700000 allocations: 41.20 MiB)

```

And here’s an alternative implementation using static vectors instead:

```julia
julia> using StaticArrays

julia> function orientation(r::SVector{3, Float64})
           R = norm(r)
           q0 = cos(0.5 * R)
           q1, q2, q3 = sin(0.5 * R) * (r / R)
           SVector(2*(q0*q2 + q1*q3), -2*(q0*q1 - q2*q3),
                   q0^2 - q1^2 - q2^2 + q3^2)
       end
orientation (generic function with 1 method)

julia> rotvec = [rand(SVector{3, Float64}) for i in 1:100000];

julia> ori = similar(rotvec);

julia> @btime $ori .= orientation.($rotvec)
  1.830 ms (0 allocations: 0 bytes)

```

That’s almost 10 times faster, uses less code, and allocates exactly zero memory.

---

_[View the full topic](https://discourse.julialang.org/t/memory-allocations-in-for-loop-variable/14332)._
