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


#1

Hello,
please consider this code:

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.


#2

You might like TimerOutputs

Alternatively, you could use @elapsed

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


#3

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?


#4

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

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:

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


#5

That works. Thank you.