# Modifying the @time macro

**URL:** https://discourse.julialang.org/t/modifying-the-time-macro/2790
**Category:** New to Julia
**Tags:** macros
**Created:** [March 21, 2017, 10:43am UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790 "2017-03-21T10:43:29Z")
**Posts on this page:** 14
**Page:** 1

<div class="post-metadata">

### Author: ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)
#### Post date: [March 21, 2017, 10:43am UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790/1 "2017-03-21T10:43:30Z")

</div>

In my example case the macro @time

```
@time (for n in 0:100 myFunction(n) end)

```

outputs

```
0.355005 seconds (8 allocations: 1.188 KB)

```

I would like to have a macro @Time which outputs

```
(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?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 21, 2017, 11:15am UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790/2 "2017-03-21T11:15:34Z")

</div>

Something like

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

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [March 21, 2017, 11:55am UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790/3 "2017-03-21T11:55:14Z")

</div>

Combining the `@show` and `@time` macro, with some modifications:

```julia
julia> macro showtime(ex)
           println(sprint(Base.show_unquoted, ex))
           @time ex
       end

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 21, 2017, 12:01pm UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790/4 "2017-03-21T12:01:33Z")

</div>

> [@fredrikekre](#):
>
> macro showtime(ex)  
> println(sprint(Base.show\_unquoted, ex))  
> @time ex  
> end

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

```julia
macro time2(ex)
    quote
        println($(Meta.quot(ex)))
        @time $ex
    end
end

```

---

<div class="post-metadata">

### Author: ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)
#### Post date: [March 21, 2017, 12:16pm UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790/5 "2017-03-21T12:16:02Z")

</div>

Ah, right, I updated my example such that it also works.  
Edit: It doesnt. Your example is better 😃

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [March 21, 2017, 12:23pm UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790/6 "2017-03-21T12:23:12Z")

</div>

`$ex` has to be `esc`aped…

---

<div class="post-metadata">

### Author: ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)
#### Post date: [March 21, 2017, 1:27pm UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790/7 "2017-03-21T13:27:07Z")

</div>

Thanks Tamas, however

```
macro time2(ex)
    quote
        println($(Meta.quot(ex)))
        @time esc($(ex))
    end
end
myfunc(n::Int) = sleep(0.00001*n)
@time2 (for n in 0:100 myfunc(n) end)

```

outputs

```
for n = 0:100 # long\path\...\myfunc.jl, line 15:
    myFunc(n)
end
0.199292 seconds (612 allocations: 37.984 KB)

```

The problem here is the comment

```
# long\path\...\myfunc.jl, line 15:

```

which I do not want. How can I get rid of it?

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [March 21, 2017, 1:44pm UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790/8 "2017-03-21T13:44:23Z")

</div>

`$(esc(ex))` (not about line node filtering)

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [March 21, 2017, 1:49pm UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790/9 "2017-03-21T13:49:42Z")

</div>

```julia
using MacroTools
macro time2(ex)
    quote
        println($(Meta.quot(rmlines(ex))))
        @time $(esc(ex))
    end
end

```

Look at `MacroTools.jl`, it is a really great package with lots of wonderful utilities like this.

> **[GitHub - FluxML/MacroTools.jl: MacroTools provides a library of tools for...](https://github.com/FluxML/MacroTools.jl)**
>
> MacroTools provides a library of tools for working with Julia code and expressions. - GitHub - FluxML/MacroTools.jl: MacroTools provides a library of tools for working with Julia code and expressions.

---

<div class="post-metadata">

### Author: ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)
#### Post date: [March 21, 2017, 2:53pm UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790/10 "2017-03-21T14:53:09Z")

</div>

Hmm… this does not make any difference  
to what I described in [this post](https://discourse.julialang.org/t/modifying-the-time-macro/2790/7).

---

<div class="post-metadata">

### Author: ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)
#### Post date: [March 21, 2017, 3:21pm UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790/11 "2017-03-21T15:21:03Z")

</div>

Works perfectly for me.

---

<div class="post-metadata">

### Author: ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)
#### Post date: [March 21, 2017, 8:02pm UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790/12 "2017-03-21T20:02:57Z")

</div>

To make sure that it is not an issue with my installation  
I run the same code on SageMathCloud in a notebook.

Instead of the comment as it appears on my computer

```
# long\path\...\myfunc.jl, line 15:

```

I now see at the corresponding position

```
# In[1], line 11:

```

Insignificant differences aside this shows that  
that the problem is not a mirage.

---

<div class="post-metadata">

### Author: ![jkroso](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jkroso/32/220909_2.png) [@jkroso](https://discourse.julialang.org/u/jkroso)
#### Post date: [December 26, 2018, 6:43am UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790/13 "2018-12-26T06:43:30Z")

</div>

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.

```julia
macro time2(expr)
  macroexpand( __module__ , Expr(:macrocall, getfield(Main, Symbol("@time")), __source__ , expr))
end

```

---

<div class="post-metadata">

### Author: ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)
#### Post date: [December 26, 2018, 5:10pm UTC](https://discourse.julialang.org/t/modifying-the-time-macro/2790/14 "2018-12-26T17:10:40Z")

</div>

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.

> **[GitHub - chakravala/SyntaxTree.jl: Toolset for modifying Julia AST and...](https://github.com/chakravala/SyntaxTree.jl)**
>
> Toolset for modifying Julia AST and characteristic values - GitHub - chakravala/SyntaxTree.jl: Toolset for modifying Julia AST and characteristic values
