# Allocations when constructing a matrix from columns

**URL:** <https://discourse.julialang.org/t/allocations-when-constructing-a-matrix-from-columns/36795>\
**Category:** Performance\
**Tags:** memory-allocation\
**Created:** [March 31, 2020, 10:59am UTC](https://discourse.julialang.org/t/allocations-when-constructing-a-matrix-from-columns/36795 "2020-03-31T10:59:18Z")\
**Posts on this page:** 1\
**Showing post:** 32

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [April 1, 2020, 11:48am UTC](https://discourse.julialang.org/t/allocations-when-constructing-a-matrix-from-columns/36795/32 "2020-04-01T11:48:12Z")

</div>

> [@mht](#):
>
> My issue was that the simplest way I could think of of defining a single matrix of 9 elements ended up being 13 allocations, which is almost 150% of the number of elements. This was very surprising to me, and having spent some time in my life looking at compiler output I do have a feeling for when the compiler is doing the Wrong Thing, and in this case it 100% was.
> 
> Now, using the broadcasting version suggested by @DNF did fix it, I would like to better understand _why_ Julia generated sub-par code before, so that I don’t have to flip every stone in my program or run benchmark every time I write code just to avoid having bad code generated. Furthermore, since I’ve already ran into these problems with the optimizer _for the primal_ , and I’m ADing this, I’d like to make the job of the compiler as simple as possible so that it can do what it does best. This includes not having allocations unless your really need to, even if you can’t immediately spot it as a hot spot on a profile.

yuyichao and Keno’s comments in [this thread](https://discourse.julialang.org/t/why-is-julias-generated-assembly-more-complicated-than-swifts-for-a-simple-function/35549) may help; I found them very interesting and informative.

Zygote is focused on machine learning, where 1 microsecond of overhead per operation is negligible in the face of gigantic array operations. I’ve often found ForwardDiff to be a better choice when doing lots of small operations (especially with StaticArrays, which it plays nicely with). Have you considered it?

---

_[View the full topic](https://discourse.julialang.org/t/allocations-when-constructing-a-matrix-from-columns/36795)._
