# Function uses huge memory

**URL:** <https://discourse.julialang.org/t/function-uses-huge-memory/15333>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 22, 2018, 9:23am UTC](https://discourse.julialang.org/t/function-uses-huge-memory/15333 "2018-09-22T09:23:29Z")\
**Posts on this page:** 12\
**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:** [September 22, 2018, 9:23am UTC](https://discourse.julialang.org/t/function-uses-huge-memory/15333/1 "2018-09-22T09:23:29Z")

</div>

I can not figure out as to why the function given below uses huge memory allocations.

```julia
function findpi(n::Int64)::Float64
    inside = 0
    x,y=0.0,0.0
       for i in 1:n
       		x, y = rand(2)
            
       		inside += (x^2 + y^2) <= 1 ? 1 : 0
       	end
       	4 * inside / n
end

@time findpi(10000000)
@time findpi(10000000)
@time findpi(10000000)

 0.556831 seconds (10.04 M allocations: 917.497 MiB, 11.59% gc time)
  0.461873 seconds (10.00 M allocations: 915.528 MiB, 10.93% gc time)
  0.486833 seconds (10.00 M allocations: 915.528 MiB, 11.24% gc time)

```

---

<div class="post-metadata">

**Author:** ![LeoK987](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leok987/32/4880_2.png) [@LeoK987](https://discourse.julialang.org/u/LeoK987)\
**Post date:** [September 22, 2018, 9:56am UTC](https://discourse.julialang.org/t/function-uses-huge-memory/15333/2 "2018-09-22T09:56:54Z")

</div>

Don’t mix Int and Float64 in the calculations.

---

<div class="post-metadata">

**Author:** ![jbrea](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbrea/32/3879_2.png) [@jbrea](https://discourse.julialang.org/u/jbrea)\
**Post date:** [September 22, 2018, 9:57am UTC](https://discourse.julialang.org/t/function-uses-huge-memory/15333/3 "2018-09-22T09:57:59Z")

</div>

Here is an alternative

```julia
julia> function findpi(n)
           inside = 0.
           for i in 1:n
               inside += rand()^2 + rand()^2 <= 1 ? 1. : 0.
           end
           4 * inside / n
       end
findpi (generic function with 1 method)

julia> @benchmark findpi(10000000)
BenchmarkTools.Trial: 
  memory estimate: 0 bytes
  allocs estimate: 0
  --------------
  minimum time: 43.458 ms (0.00% GC)
  median time: 44.101 ms (0.00% GC)
  mean time: 44.429 ms (0.00% GC)
  maximum time: 54.051 ms (0.00% GC)
  --------------
  samples: 113
  evals/sample: 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:** [September 22, 2018, 10:05am UTC](https://discourse.julialang.org/t/function-uses-huge-memory/15333/4 "2018-09-22T10:05:26Z")

</div>

Thank you both @LeoK987 and @jbrea for your quick and wonderful response.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [September 22, 2018, 10:58am UTC](https://discourse.julialang.org/t/function-uses-huge-memory/15333/5 "2018-09-22T10:58:15Z")

</div>

`rand(2)` allocates a vector, which you deconstruct immediately  
`x,y = rand(), rand()` should solve that problem.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [September 22, 2018, 11:04am UTC](https://discourse.julialang.org/t/function-uses-huge-memory/15333/6 "2018-09-22T11:04:13Z")

</div>

Also note that the mixing of float and Int was not a problem in your original code. Everything was type stable, as `inside` started out as an int and stayed an Int.

---

<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:** [September 23, 2018, 9:12am UTC](https://discourse.julialang.org/t/function-uses-huge-memory/15333/8 "2018-09-23T09:12:47Z")

</div>

Thanks @baggepinnen.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 23, 2018, 5:15pm UTC](https://discourse.julialang.org/t/function-uses-huge-memory/15333/9 "2018-09-23T17:15:45Z")

</div>

It seems like we are missing a generator version of `rand(N)`.

---

<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:** [September 23, 2018, 5:24pm UTC](https://discourse.julialang.org/t/function-uses-huge-memory/15333/10 "2018-09-23T17:24:16Z")

</div>

```julia
a, b, c, d, e = Iterators.cycle((randn() for i in 1));

```

works.

---

<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:** [September 23, 2018, 5:35pm UTC](https://discourse.julialang.org/t/function-uses-huge-memory/15333/11 "2018-09-23T17:35:57Z")

</div>

> [@LeoK987](#):
>
> Don’t mix Int and Float64 in the calculations.

In this case they were not mixed. And, in general, there’s nothing wrong with mixing them, either.

---

<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:** [September 23, 2018, 5:47pm UTC](https://discourse.julialang.org/t/function-uses-huge-memory/15333/12 "2018-09-23T17:47:08Z")

</div>

> [@jbrea](#):
>
> ```julia
> function findpi(n)
> inside = 0.
> for i in 1:n
> inside += rand()^2 + rand()^2 <= 1 ? 1. : 0.
> end
> 4 * inside / n
> end
> 
> ```

Using floats is not optimal here. Use `Int`s, like this:

```julia
function findpi2(n)
    inside = 0
    for i in 1:n
        inside += rand()^2 + rand()^2 <= 1 ? 1 : 0
    end
    return 4 * inside / n
end

```

or, equally fast (but more elegant, IMHO):

```julia
function findpi3(n)
    inside = 0
    for i in 1:n
        inside += (rand()^2 + rand()^2 <= 1)
    end
    return 4 * inside / n
end

```

```julia
julia> @btime findpi(10^7)
  38.940 ms (0 allocations: 0 bytes)
3.1421128

julia> @btime findpi2(10^7)
  32.242 ms (0 allocations: 0 bytes)
3.1416336

julia> @btime findpi3(10^7)
  32.220 ms (0 allocations: 0 bytes)
3.1418104

```

---

<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:** [September 24, 2018, 5:13am UTC](https://discourse.julialang.org/t/function-uses-huge-memory/15333/13 "2018-09-24T05:13:21Z")

</div>

@DNF Thanks
