# Timing Julia execution

**URL:** https://discourse.julialang.org/t/timing-julia-execution/18118
**Category:** New to Julia
**Created:** [November 28, 2018, 10:07pm UTC](https://discourse.julialang.org/t/timing-julia-execution/18118 "2018-11-28T22:07:17Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [November 28, 2018, 10:07pm UTC](https://discourse.julialang.org/t/timing-julia-execution/18118/1 "2018-11-28T22:07:17Z")

</div>

This is probably a silly question, but I’d like to time a _sequence_ of commands. I know that I can use `@time statement`, but how do I time a _sequence_ of statements?

```nohighlight
@time
statement 1
statement 2
…
statement n

```

So – how do I _bracket_ the statements so that Julia times the total execution time?  
[I dug up some MATLAB code from 1993 or so… at that time, MATLAB took 7 hours to solve the problem on a DECStation 3000… Today, MATLAB took ca. 9 seconds to solve the same problem on my desktop… I did a crude rewrite into Julia, and my guesstimate is ca. 5 seconds in Julia, but I’d like to know.]

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [November 28, 2018, 10:13pm UTC](https://discourse.julialang.org/t/timing-julia-execution/18118/2 "2018-11-28T22:13:29Z")

</div>

You can time an entire block of statements like this:

```julia
@time begin
   ...
   ...
end

```

Or you can put your code into a function and time calling that function. Using functions will probably make your code faster and easier to use, so I’d recommend that anyway.

You also might want to check out BenchmarkTools.jl for more accurate benchmarking.

---

<div class="post-metadata">

### Author: ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)
#### Post date: [November 28, 2018, 10:32pm UTC](https://discourse.julialang.org/t/timing-julia-execution/18118/3 "2018-11-28T22:32:45Z")

</div>

Thanks. I think I tried something similar in the past, but I must have done something wrong then…

The execution takes at least a handful of seconds, so I’d like to do it like you suggest first.

---

<div class="post-metadata">

### Author: ![affans](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/affans/32/11911_2.png) [@affans](https://discourse.julialang.org/u/affans)
#### Post date: [November 28, 2018, 11:04pm UTC](https://discourse.julialang.org/t/timing-julia-execution/18118/4 "2018-11-28T23:04:08Z")

</div>

Write all relevant code in functions to avoid the global scope. Global scope is bad in Julia and will slow your program.

Next you have to run `@time func()` twice. The first time you run it, you are also capturing compile time. The second and any subsequeuent run should tell you the “true” runtime of your code.

Alternatively use @btime from the BenchMarkTools package.
