peter-b
peter-b

Reputation: 4191

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?

Upvotes: 4

Views: 1540

Answers (2)

StefanKarpinski
StefanKarpinski

Reputation: 33259

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.

Upvotes: 10

Dan Getz
Dan Getz

Reputation: 18217

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!

Upvotes: 2

Related Questions