# How to save @time output in a file or variable?

**URL:** <https://discourse.julialang.org/t/how-to-save-time-output-in-a-file-or-variable/11218>\
**Category:** New to Julia\
**Created:** [May 29, 2018, 4:29am UTC](https://discourse.julialang.org/t/how-to-save-time-output-in-a-file-or-variable/11218 "2018-05-29T04:29:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alirezamecheng](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@alirezamecheng](https://discourse.julialang.org/u/alirezamecheng)\
**Post date:** [May 29, 2018, 4:29am UTC](https://discourse.julialang.org/t/how-to-save-time-output-in-a-file-or-variable/11218/1 "2018-05-29T04:29:28Z")

</div>

Hello,  
please consider this code:

```julia
for N = [10 20 30]
for C = [0.1 0.2]
@time my_function(N,C);
end 
end

```

The `my_function` is a defined function.  
My question is How I can save `@time ` output in a file so that after running the code I can access the results in a separate file like `timings.txt`? or if it is possible to save the time output in a variable so that it can be used later?  
Thank you for your time.

---

<div class="post-metadata">

**Author:** ![JackDevine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackdevine/32/1048_2.png) [@JackDevine](https://discourse.julialang.org/u/JackDevine)\
**Post date:** [May 29, 2018, 4:53am UTC](https://discourse.julialang.org/t/how-to-save-time-output-in-a-file-or-variable/11218/2 "2018-05-29T04:53:22Z")

</div>

You might like TimerOutputs

> **[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.

Alternatively, you could use `@elapsed`

```julia
my_function(x,y) = x+y
my_function(1,0.1) # Warm up.
timings = Array{Float64}(3,2)
for (i,N) = enumerate([10 20 30])
    for (j,C) = enumerate([0.1 0.2])
        timings[i,j] = @elapsed my_function(N,C);
    end 
end

```

```julia
julia> timings
3×2 Array{Float64,2}:
 6.1e-8 2.3e-8
 1.7e-8 1.7e-8
 6.7e-8 1.7e-8

```

And then if you want to save it, just use `writedlm`.

I personally prefer TimerOutputs, because it does some quite useful statistics for you automatically. It all depends on your taste though.

---

<div class="post-metadata">

**Author:** ![alirezamecheng](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@alirezamecheng](https://discourse.julialang.org/u/alirezamecheng)\
**Post date:** [May 29, 2018, 5:10am UTC](https://discourse.julialang.org/t/how-to-save-time-output-in-a-file-or-variable/11218/3 "2018-05-29T05:10:20Z")

</div>

That was a great help. The package is useful but my problem is this package is not installed on my system and I can`t install it either.  
so is there any way to do it in another form, not using any special package?

---

<div class="post-metadata">

**Author:** ![JackDevine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackdevine/32/1048_2.png) [@JackDevine](https://discourse.julialang.org/u/JackDevine)\
**Post date:** [May 29, 2018, 5:19am UTC](https://discourse.julialang.org/t/how-to-save-time-output-in-a-file-or-variable/11218/4 "2018-05-29T05:19:12Z")

</div>

You can install any package with `Pkg.add`. In this case, you just need to run

```julia
Pkg.add("TimerOutputs")

```

Julia is a little different from systems like MATLAB in that you will need to install packages even to do simple tasks.

If you don’t want to use any packages, then I think that the solution that I gave above should work for you. You might also like these functions/macros:

```julia
help?> tic
search: tic tick_params ticklabel_format zticks yticks xticks issticky

  tic()

  Set a timer to be read by the next call to toc or toq. The macro call @time
  expr can also be used to time evaluation.

  julia> tic()
  0x0000c45bc7abac95
  
  julia> sleep(0.3)
  
  julia> toc()
  elapsed time: 0.302745944 seconds
  0.302745944

help?> toc
search: toc touch autoscale getsockname trylock TCPSocket bitbroadcast

  toc()

  Print and return the time elapsed since the last tic. The macro call @time
  expr can also be used to time evaluation.

  julia> tic()
  0x0000c45bc7abac95
  
  julia> sleep(0.3)
  
  julia> toc()
  elapsed time: 0.302745944 seconds
  0.302745944

help?> @allocated
  @allocated

  A macro to evaluate an expression, discarding the resulting value, instead
  returning the total number of bytes allocated during evaluation of the
  expression. Note: the expression is evaluated inside a local function,
  instead of the current context, in order to eliminate the effects of
  compilation, however, there still may be some allocations due to JIT
  compilation. This also makes the results inconsistent with the @time macros,
  which do not try to adjust for the effects of compilation.

  See also @time, @timev, @timed, and @elapsed.

  julia> @allocated rand(10^6)
  8000080

help?> @timed
  @timed

  A macro to execute an expression, and return the value of the expression,
  elapsed time, total bytes allocated, garbage collection time, and an object
  with various memory allocation counters.

  See also @time, @timev, @elapsed, and @allocated.

  julia> val, t, bytes, gctime, memallocs = @timed rand(10^6);
  
  julia> t
  0.006634834
  
  julia> bytes
  8000256
  
  julia> gctime
  0.0055765
  
  julia> fieldnames(typeof(memallocs))
  9-element Array{Symbol,1}:
   :allocd
   :malloc
   :realloc
   :poolalloc
   :bigalloc
   :freecall
   :total_time
   :pause
   :full_sweep
  
  julia> memallocs.total_time
  5576500

```

---

<div class="post-metadata">

**Author:** ![alirezamecheng](https://avatars.discourse-cdn.com/v4/letter/a/e95f7d/32.png) [@alirezamecheng](https://discourse.julialang.org/u/alirezamecheng)\
**Post date:** [May 29, 2018, 5:31am UTC](https://discourse.julialang.org/t/how-to-save-time-output-in-a-file-or-variable/11218/5 "2018-05-29T05:31:35Z")

</div>

That works. Thank you.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [April 13, 2021, 3:12pm UTC](https://discourse.julialang.org/t/how-to-save-time-output-in-a-file-or-variable/11218/6 "2021-04-13T15:12:16Z")

</div>

For those that nowaday use [BenchmarkTools](https://github.com/JuliaCI/BenchmarkTools.jl):

```julia
using BenchmarkTools
t = @benchmark myfunction($arg)
minTime = minimum(t.times)

```

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [April 13, 2021, 3:47pm UTC](https://discourse.julialang.org/t/how-to-save-time-output-in-a-file-or-variable/11218/7 "2021-04-13T15:47:30Z")

</div>

The right answer to the original question at that time and today would probably be either `@elapsed` or `@timed` both from `Base`.
