Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Julia: access @time timing and memory allocation values from within code

I am profiling my Julia application, in particular the execution time and memory allocation of function calls. I would like to automate storage of this information to a database so it can run without supervision.

The information I want to store is that returned by @time, of the form:

@time some_function()
 86.278909 seconds (6.94 M allocations: 383.520 MB, 0.08% gc time)

Is it possible to access this information from within the code itself, rather than it just being printed out?

I know I can access the time component using tic() and toq(), but what about the memory allocation?

like image 895
peter-b Avatar asked Sep 18 '25 23:09

peter-b


2 Answers

There is a @timed macro that gives you all of this information:

julia> @timed sleep(1)
(nothing,1.00417,624,0.0,Base.GC_Diff(624,0,0,13,0,0,0,0,0))

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.
like image 170
StefanKarpinski Avatar answered Sep 23 '25 10:09

StefanKarpinski


First, Julia allows very good access to the internals of everything. So if something does something, just look inside how. In the case of the @time macro, looking inside is done with the macroexpand, in the REPL:

macroexpand(:(@time some_function()))

Having done so, the equivalent for tic() and toq() for allocations are before = Base.gc_num and diff = Base.GC_Diff(Base.gc_num(),before).

The diff variable now holds the allocations statistics.

Base.gc_alloc_count(diff) gives the allocation count for example.

Cheers!

like image 38
Dan Getz Avatar answered Sep 23 '25 12:09

Dan Getz