(for n in 0:100 myFunction(n) end)
0.355005 seconds (8 allocations: 1.188 KB)
In other words @Time prints the argument of @time
and the result of @time. I have read the docs on
metaprogramming however did not succeed in writing
this macro. Any help?
macro time2(ex)
quote
local stats = Base.gc_num()
local elapsedtime = time_ns()
local val = $(esc(ex))
elapsedtime = time_ns() - elapsedtime
local diff = Base.GC_Diff(Base.gc_num(), stats)
println($(Meta.quot(ex))) # print
Base.time_print(elapsedtime, diff.allocd, diff.total_time,
Base.gc_alloc_count(diff))
val
end
end
Note that most of the body is from the definition of @time (which you can look up with eg @edit), I just inserted a line for printing and qualified some functions from Base with their module name.
I don’t think this does what the OP asked for — it will print ex at the time of macro expansion, and return the value otherwise. If the intention is to reuse @time, consider
macro time2(ex)
quote
println($(Meta.quot(ex)))
@time $ex
end
end
The solution listed here didn’t work for my case because of some complicated way escaping was happening which I never figured out. Anyway I ended up using macroexpand directly and the escaping issues went away. Here is how to use macroexpand in cases like this.
macro time2(expr)
macroexpand(__module__, Expr(:macrocall, getfield(Main, Symbol("@time")), __source__, expr))
end
You can also use SyntaxTree.linefilter! from my package, which is faster, if you need a performance critical tool that removes the line comnets from Julia expressions.