# Reduce allocations for shaped views

**URL:** <https://discourse.julialang.org/t/reduce-allocations-for-shaped-views/20757>\
**Category:** Performance\
**Created:** [February 13, 2019, 4:49pm UTC](https://discourse.julialang.org/t/reduce-allocations-for-shaped-views/20757 "2019-02-13T16:49:03Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [February 13, 2019, 7:50pm UTC](https://discourse.julialang.org/t/reduce-allocations-for-shaped-views/20757/6 "2019-02-13T19:50:47Z")

</div>

That solution seems broken (`d1` doesn’t do the same thing as `d0`). I’m sharing a post I recently wrote below, which I think is really important, and at the same time very simple to do.

As for how to speed this up: I often find that the easiest and fastest way is to just stick to for loops instead of views and reshapes. Just remember to do `@inbounds`, and to [acess arrays in memory order, along columns](https://docs.julialang.org/en/latest/manual/performance-tips/#Access-arrays-in-memory-order,-along-columns-1). (Note: due to caching/data locality, you might sometimes benefit from reorganizing your data.)

> [@Can I make my code faster with parallelism, or just plain better coding?](https://discourse.julialang.org/t/can-i-make-my-code-faster-with-parallelism-or-just-plain-better-coding/20613/17):
>
> When optimizing or refactoring code like this, I always employ a simple form of [Golden Master Testing](https://en.wikipedia.org/wiki/Characterization_test). Basically, all you’re testing then, is that your working version is equal to the original version. Whether it was correct or not to begin with, who knows, but at least it works the same. This is usually trivial to set up. Example: Random.seed!(0) # fix random seed, so you always get the same data A = rand(100, 100) # create input data B = complex\_algorithm(A) # output data …

---

_[View the full topic](https://discourse.julialang.org/t/reduce-allocations-for-shaped-views/20757)._
