# Non-allocating loop over a set of structs

**URL:** https://discourse.julialang.org/t/non-allocating-loop-over-a-set-of-structs/25643
**Category:** Performance
**Created:** [June 25, 2019, 10:17am UTC](https://discourse.julialang.org/t/non-allocating-loop-over-a-set-of-structs/25643 "2019-06-25T10:17:56Z")
**Posts on this page:** 1
**Showing post:** 6

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [June 25, 2019, 12:44pm UTC](https://discourse.julialang.org/t/non-allocating-loop-over-a-set-of-structs/25643/6 "2019-06-25T12:44:22Z")

</div>

> [@fedoroff](#):
>
> I still do not understand why the result of the loop is so different from the result of unrolling. I thought that under the hood Julia does exactly the same.

Why did you think a loop and unrolled code would be exactly the same? Unrolling something like `for i in 1:1000000` is clearly not a good idea. A loop, by its definition, is executing the same code multiple times. Since `samples` contains heterogeneous type, “the same code” needs to be something generic that can handle things with different types.

Of course, a loop _can_ be unrolled but it is not obvious for code like this when doing so is beneficial.

---

_[View the full topic](https://discourse.julialang.org/t/non-allocating-loop-over-a-set-of-structs/25643)._
