# Different timing results when putting tic() toc() inside or outside a function

**URL:** <https://discourse.julialang.org/t/different-timing-results-when-putting-tic-toc-inside-or-outside-a-function/12806>\
**Category:** Performance\
**Created:** [August 1, 2018, 11:36pm UTC](https://discourse.julialang.org/t/different-timing-results-when-putting-tic-toc-inside-or-outside-a-function/12806 "2018-08-01T23:36:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Norman](https://avatars.discourse-cdn.com/v4/letter/n/97f17d/32.png) [@Norman](https://discourse.julialang.org/u/Norman)\
**Post date:** [August 1, 2018, 11:36pm UTC](https://discourse.julialang.org/t/different-timing-results-when-putting-tic-toc-inside-or-outside-a-function/12806/1 "2018-08-01T23:36:45Z")

</div>

Here is a minimal working example to illustrate what I am trying to ask:

```
function test(n::Int)
tic()
i = 0
while (i <= n)
    A = randn(100,100)
    B = randn(100,100)
    C = A*B
    i+=1
end
toc()
end

test(100)

```

I always time my functions by putting `@time` in front of the function call (like `@time test(100)`). However, I realized that if I put `tic()` and `toc()` inside the function definition, then the timing result could be quite different.

For the example above, I get `elapsed time: 0.033487528 seconds`. However, if I remove the `tic()` and `toc()` and use

```
tic()
test(100)
toc()

```

then I get `elapsed time: 0.047568188 seconds`. (In both case, the code is run at least twice to avoid the compilation time.)

I don’t really understand what influences the gap between the two ways of timing. I am asking this because in my actual project, I encounter a timing difference of about 0.66 second and 0.015 second, which is quite significant! What are the common reasons for making this gap large? Thanks!

---

<div class="post-metadata">

**Author:** ![djsegal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/djsegal/32/13752_2.png) [@djsegal](https://discourse.julialang.org/u/djsegal)\
**Post date:** [August 2, 2018, 1:44am UTC](https://discourse.julialang.org/t/different-timing-results-when-putting-tic-toc-inside-or-outside-a-function/12806/2 "2018-08-02T01:44:01Z")

</div>

Is this related to:

- [“Don’t benchmark in global scope”](https://discourse.julialang.org/search?q=global%20scope%20performance)

?

* * *

 ![01%20PM](https://global.discourse-cdn.com/julialang/original/3X/8/f/8f9cb578be64656293da487f81a3d94c6ad5dd13.png)

---

<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:** [August 2, 2018, 5:34am UTC](https://discourse.julialang.org/t/different-timing-results-when-putting-tic-toc-inside-or-outside-a-function/12806/3 "2018-08-02T05:34:47Z")

</div>

Don’t (usually) use tic and toc; do use BenchmarkTooks.jl for micro-benchmarking, or `@time` if the function runs for long enough (seconds).
