# Reducing Allocation

**URL:** https://discourse.julialang.org/t/reducing-allocation/28078
**Category:** General Usage
**Tags:** question
**Created:** [August 28, 2019, 5:17am UTC](https://discourse.julialang.org/t/reducing-allocation/28078 "2019-08-28T05:17:52Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)
#### Post date: [August 28, 2019, 5:17am UTC](https://discourse.julialang.org/t/reducing-allocation/28078/1 "2019-08-28T05:17:52Z")

</div>

Out of curiosity, is it possible to make the function zero allocation? The output excludes compile time.

```julia
using StatsPlots,Random, BenchmarkTools
# using TimerOutputs
Random.seed!(123)
function createPath(S, r, sigma, T, n,sim)
    h = T / n
    u = exp((r - sigma^2/2)*h + sigma * sqrt(h))
    d = exp((r - sigma^2/2)*h - sigma * sqrt(h))

    p_star = (exp(r*h) - d) / (u - d)

    path = zeros(n+2) #Array{Float64}(undef,n + 2)

    #add in the starting price
    path[1] = S
    p = plot() 
    
    for i in 1:sim
        
        for j in 2:n+2
# if rand() < p_star
                #then we go up
                path[j] = path[j-1] * (rand() < p_star ? u : d)
# path[2:n+2] = path[1:n+1] .* [x < p_star ? u : d for x in rand(n+1)]'
            #lse 
# path[k] = path[k-1] * d
# end
        end
        plot!(p, path,w=1,primary=true,legend=false);  
        
    end
    plot!(p, title="Stock Price Paths", xlabel="Period", ylabel="Price",size=[1000,500],w=1);
    return p
end
@time createPath(100.0, 0.08, 0.3, 1.0, 1000,100)
  0.120412 seconds (236.19 k allocations: 14.836 MiB, 11.75% gc time)
```

---

<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: [August 28, 2019, 5:26am UTC](https://discourse.julialang.org/t/reducing-allocation/28078/2 "2019-08-28T05:26:53Z")

</div>

Plotting is surely going to allocate.

---

<div class="post-metadata">

### Author: ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)
#### Post date: [August 28, 2019, 5:35am UTC](https://discourse.julialang.org/t/reducing-allocation/28078/3 "2019-08-28T05:35:51Z")

</div>

@kristoffer.carlsson: Thanks. I managed to check that by using TimerOutput package.

---

<div class="post-metadata">

### Author: ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)
#### Post date: [August 28, 2019, 7:06am UTC](https://discourse.julialang.org/t/reducing-allocation/28078/4 "2019-08-28T07:06:16Z")

</div>

You also create a 1000-element vector inside the function, which allocates. If you want to get rid of that, you need to pre-allocate it and pass it into the function.

Still, that’s nothing compared to the plotting, I guess.

---

<div class="post-metadata">

### Author: ![Ajaychat3](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@Ajaychat3](https://discourse.julialang.org/u/Ajaychat3)
#### Post date: [August 28, 2019, 8:40am UTC](https://discourse.julialang.org/t/reducing-allocation/28078/5 "2019-08-28T08:40:14Z")

</div>

@DNF: absolutely true that plotting uses much bigger allocation than the vector.
