# How to do flexible timing in Julia?

**URL:** https://discourse.julialang.org/t/how-to-do-flexible-timing-in-julia/66577
**Category:** General Usage
**Created:** [August 17, 2021, 11:50pm UTC](https://discourse.julialang.org/t/how-to-do-flexible-timing-in-julia/66577 "2021-08-17T23:50:54Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![CRquantum](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crquantum/32/27824_2.png) [@CRquantum](https://discourse.julialang.org/u/CRquantum)
#### Post date: [August 17, 2021, 11:50pm UTC](https://discourse.julialang.org/t/how-to-do-flexible-timing-in-julia/66577/1 "2021-08-17T23:50:54Z")

</div>

Another stupid question, in Julia, how to do timing on various parts in the code and display them correspondingly?

For example, a Fortran code like below,

```
call cpu_time(t0)
 blablabala
call cpu_time(t1)
write(6,*) 't1-t0=', t1-t0
 blabalabla
call cpu_time(t2)
 blablabala
call cpu_time(t3)
write(6,*) 't3-t2=', t3-t2  

```

Now, from the write(6,\*), I can see clearly how much time it took between t0 and t1, t2 and t3.

In Julia is there a similar way so that I can do things similar above?  
Like, I know, in Julia one can do

```
@time begin
 blablabla 
 end

```

But is there a command such that I can store the time it took in the above block?  
Because if I were to do timing for different blocks in the code, I need to store the time it took for each of the block. Finally, I can print those time altogether.

Thanks in advance! Apologize for the stupid question.

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [August 18, 2021, 12:00am UTC](https://discourse.julialang.org/t/how-to-do-flexible-timing-in-julia/66577/2 "2021-08-18T00:00:04Z")

</div>

You can use the [`@profile`](https://docs.julialang.org/en/v1/manual/profile/) macro to do something similar without having to manually instrument your code.

If you really need times rather than sampled frequencies, you can use [TimerOutputs.jl](https://github.com/KristofferC/TimerOutputs.jl).

---

<div class="post-metadata">

### Author: ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)
#### Post date: [August 18, 2021, 12:06am UTC](https://discourse.julialang.org/t/how-to-do-flexible-timing-in-julia/66577/3 "2021-08-18T00:06:25Z")

</div>

If you want to do things _very_ manually, you can use [`time_ns`](https://docs.julialang.org/en/v1/base/base/#Base.time_ns):

```julia
julia> t0 = time_ns(); sleep(0.5); t1 = time_ns();

julia> println("My long calculations took $((t1 - t0) / 1e9) seconds")
My long calculations took 0.50197262 seconds

```

The macro [`@time`](https://docs.julialang.org/en/v1/base/base/#Base.@time) is more useful for doing some simple benchmarking, but at least of the order of ~milliseconds:

```julia
julia> @time sleep(0.5)
  0.501585 seconds (5 allocations: 144 bytes)

```

Read carefully the docstring to understand some caveats. Other related macros, [`@timev`](https://docs.julialang.org/en/v1/base/base/#Base.@timev), [`@timed`](https://docs.julialang.org/en/v1/base/base/#Base.@timed), [`@elapsed`](https://docs.julialang.org/en/v1/base/base/#Base.@elapsed), and [`@allocated`](https://docs.julialang.org/en/v1/base/base/#Base.@allocated), may be useful, depending on what you really need. `@elapsed` may be closer to what you asked:

```julia
julia> t = @elapsed sleep(0.5)
0.501572848

```

However for more fine-grained benchmarking you should look into [`BenchmarkTools.jl`](https://github.com/JuliaCI/BenchmarkTools.jl). For profiling, instead, see the message above.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [August 18, 2021, 12:49am UTC](https://discourse.julialang.org/t/how-to-do-flexible-timing-in-julia/66577/4 "2021-08-18T00:49:49Z")

</div>

> **[GitHub - KristofferC/TimerOutputs.jl: Formatted output of timed sections in...](https://github.com/KristofferC/TimerOutputs.jl)**
>
> Formatted output of timed sections in Julia. Contribute to KristofferC/TimerOutputs.jl development by creating an account on GitHub.

Imo, for that, this is the best
