# Performance of filling an array

**URL:** https://discourse.julialang.org/t/performance-of-filling-an-array/22788
**Category:** General Usage
**Tags:** performance
**Created:** [April 5, 2019, 9:38am UTC](https://discourse.julialang.org/t/performance-of-filling-an-array/22788 "2019-04-05T09:38:55Z")
**Posts on this page:** 1
**Showing post:** 5

<div class="post-metadata">

### Author: ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)
#### Post date: [April 5, 2019, 3:29pm UTC](https://discourse.julialang.org/t/performance-of-filling-an-array/22788/5 "2019-04-05T15:29:35Z")

</div>

As far as why some of these forms allow for constant propagation and some don’t, it appears as though there was a [strange edge case in the compiler](https://discourse.julialang.org/t/performance-degradation-of-fill-in-latest-julia-0-7-dev/9648/3) back in the 0.7 timeframe that prompted a [simple workaround](https://github.com/JuliaLang/julia/pull/26418). That’s no longer necessary.

> <https://github.com/JuliaLang/julia/pull/31626>
>
> This used to be necessary to avoid a strange edge case in the compiler, but it i…s no longer necessary -- and can now in fact cause other performance snags.
> 
> Using the test case from \[the original discourse post that prompted #26418\](https://discourse.julialang.org/t/performance-degradation-of-fill-in-latest-julia-0-7-dev/9648):
> 
> \`\`\`julia
> \# BEFORE
> julia\> @btime fill(1.0,5,5);
> 49.335 ns (1 allocation: 288 bytes)
> 
> julia\> @btime fill(0.0,5,5);
> 52.773 ns (1 allocation: 288 bytes)
> 
> \# AFTER
> julia\> @btime fill(0.0,5,5);
> 46.724 ns (1 allocation: 288 bytes)
> 
> julia\> @btime fill(1.0,5,5);
> 42.202 ns (1 allocation: 288 bytes)
> \`\`\`
> 
> Even more compelling is the case for a larger array where LLVM can exploit some sort of wider/simdier implementation for zeros when this gets inlined thanks to constant propagation:
> 
> \`\`\`julia
> \# AFTER
> julia\> A = Array{Float64}(undef, 1000, 1000);
> 
> julia\> @btime fill!($A,0.0);
> 345.103 μs (0 allocations: 0 bytes)
> 
> julia\> @btime fill!($A,1.0);
> 458.976 μs (0 allocations: 0 bytes)
> \`\`\`
> 
> Ref https://discourse.julialang.org/t/performance-of-filling-an-array/22788

---

_[View the full topic](https://discourse.julialang.org/t/performance-of-filling-an-array/22788)._
