# Variance in @elapsed while trying to measure function

**URL:** https://discourse.julialang.org/t/variance-in-elapsed-while-trying-to-measure-function/78049
**Category:** New to Julia
**Tags:** question
**Created:** [March 17, 2022, 8:48pm UTC](https://discourse.julialang.org/t/variance-in-elapsed-while-trying-to-measure-function/78049 "2022-03-17T20:48:45Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [March 17, 2022, 8:48pm UTC](https://discourse.julialang.org/t/variance-in-elapsed-while-trying-to-measure-function/78049/1 "2022-03-17T20:48:45Z")

</div>

Hello, good afternoon. Im using @elapsed to mesure time function and got some flutuation in sequence data. Is this peaks in plot gargabe collection noise?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/4/5/4543d04a76af9625b2271482a6a81c8661d728c8.png)

---

<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: [March 17, 2022, 9:19pm UTC](https://discourse.julialang.org/t/variance-in-elapsed-while-trying-to-measure-function/78049/2 "2022-03-17T21:19:07Z")

</div>

Good evening! It might be gc as you say. Consider using BenchmarkTools.jl for accurate benchmarking. E.g., using `@btime` or `@belapsed`

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [March 17, 2022, 9:45pm UTC](https://discourse.julialang.org/t/variance-in-elapsed-while-trying-to-measure-function/78049/3 "2022-03-17T21:45:30Z")

</div>

> [@baggepinnen](#):
>
> BenchmarkTools

Im testing here but it is taking a lot of time. Seems @belapsed cant run with more than 1000 iterations.

The functions with constant time was done, but seems more strange than using @elapsed

 ![image](https://global.discourse-cdn.com/julialang/original/3X/5/e/5e593b1c7fc7aec61239572891ee98403fa67285.png)

---

<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: [March 18, 2022, 4:36am UTC](https://discourse.julialang.org/t/variance-in-elapsed-while-trying-to-measure-function/78049/4 "2022-03-18T04:36:26Z")

</div>

You can control the number of evaluations BenchmarkTools.jl is using. The timing that is less than one nanosecond means that the compiler outsmarted the benchmark and performed the entire calculation at compile time. This is indicated on the first page of the docs [Home · BenchmarkTools.jl](https://juliaci.github.io/BenchmarkTools.jl/stable/)

> As a rule of thumb, if a benchmark reports that it took less than a nanosecond to perform, this hoisting probably occured. You can avoid this by referencing and dereferencing the interpolated variables

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [March 18, 2022, 1:04pm UTC](https://discourse.julialang.org/t/variance-in-elapsed-while-trying-to-measure-function/78049/5 "2022-03-18T13:04:23Z")

</div>

Both `@belapsed` and `@btime` already run your code more than once to collect statistics from various runs. There’s no need to invoke them in a loop.

---

<div class="post-metadata">

### Author: ![jcbritobr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jcbritobr/32/219275_2.png) [@jcbritobr](https://discourse.julialang.org/u/jcbritobr)
#### Post date: [March 21, 2022, 1:38pm UTC](https://discourse.julialang.org/t/variance-in-elapsed-while-trying-to-measure-function/78049/6 "2022-03-21T13:38:29Z")

</div>

But how to plot a performance curve with only one value collected?

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [March 21, 2022, 2:37pm UTC](https://discourse.julialang.org/t/variance-in-elapsed-while-trying-to-measure-function/78049/7 "2022-03-21T14:37:25Z")

</div>

Have you checked out the documentation of [BenchmarkTools.jl](https://juliaci.github.io/BenchmarkTools.jl/stable/manual/#Visualizing-benchmark-results)? You can use `@benchmark` to get an object containing the collected statistics.
