# @time every evaluation in repl

**URL:** <https://discourse.julialang.org/t/time-every-evaluation-in-repl/72275>\
**Category:** General Usage\
**Tags:** repl\
**Created:** [November 30, 2021, 7:10am UTC](https://discourse.julialang.org/t/time-every-evaluation-in-repl/72275 "2021-11-30T07:10:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [November 30, 2021, 7:10am UTC](https://discourse.julialang.org/t/time-every-evaluation-in-repl/72275/1 "2021-11-30T07:10:38Z")

</div>

Is it possible to hook the repl to `@time` every eval?

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [November 30, 2021, 9:14am UTC](https://discourse.julialang.org/t/time-every-evaluation-in-repl/72275/2 "2021-11-30T09:14:38Z")

</div>

Maybe you could do something with the `ast_transforms` variable connected to the `REPLBackend`, similar to how `softscope` is used as default?  
It seems like the default is just to insert the expression `softscopt(true)` at the start of the ast that is going to be evaluated so if it is possible to modify the call that creates the `REPLBacken`, you could then potentially create your own function to put into `ast_transforms` which does `@time`.  
Though I’m not sure if you can insert a macro in the ast or if you would need to expand it yourself, but then you could probably just look at the code for `@time` and do something similar to what they do there.

No clue about how this really works though, just got curious and had a look at the code and made some guesses.

[https://github.com/JuliaLang/julia/blob/2c2e7246373535997f9ea73186703b1f6b203e3e/stdlib/REPL/src/REPL.jl#L135-L152](https://github.com/JuliaLang/julia/blob/2c2e7246373535997f9ea73186703b1f6b203e3e/stdlib/REPL/src/REPL.jl#L135-L152)

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [November 30, 2021, 9:22am UTC](https://discourse.julialang.org/t/time-every-evaluation-in-repl/72275/3 "2021-11-30T09:22:52Z")

</div>

The most minimal way to do this is probably

```julia
julia> push!(Base.active_repl_backend.ast_transforms, ast -> :(t = time(); r = $(ast); Base.eval(Main, :(last_time = $(time() - t))); r))
2-element Vector{Any}:
 softscope (generic function with 1 method)
 #7 (generic function with 1 method)

julia> sleep(2)

julia> last_time
2.0292770862579346

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 30, 2021, 12:02pm UTC](https://discourse.julialang.org/t/time-every-evaluation-in-repl/72275/4 "2021-11-30T12:02:26Z")

</div>

@pfitzseb, would it be possible to add such useful functionality to the `startup.jl`?

Tried adding it _as is_, but REPL still not available and Julia throws `UndefVarError: active_repl_backend not defined`.

---

<div class="post-metadata">

**Author:** ![albheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albheim/32/34660_2.png) [@albheim](https://discourse.julialang.org/u/albheim)\
**Post date:** [November 30, 2021, 12:27pm UTC](https://discourse.julialang.org/t/time-every-evaluation-in-repl/72275/5 "2021-11-30T12:27:09Z")

</div>

I’m pretty sure it is possible, since for example [OhMyREPL](https://github.com/KristofferC/OhMyREPL.jl) can be added in the startup file and modify the REPL looks and behaviour.

With that said I have not looked into how one would go about it, just suggesting that can be a place to start looking 🙂

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [November 30, 2021, 12:35pm UTC](https://discourse.julialang.org/t/time-every-evaluation-in-repl/72275/6 "2021-11-30T12:35:38Z")

</div>

You can use something like

```julia
using REPL
atreplinit() do repl
    @async begin
        for i in 1:10
            if isdefined(Base, :active_repl_backend)
                push!(Base.active_repl_backend.ast_transforms, ast -> :(t = time(); r = $(ast); Base.eval(Main, :(last_time = $(time() - t))); r))
                return
            end
            sleep(0.5)
        end
        @warn "failed to install timer hook"
    end
end

```

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [November 30, 2021, 1:03pm UTC](https://discourse.julialang.org/t/time-every-evaluation-in-repl/72275/7 "2021-11-30T13:03:55Z")

</div>

@pfitzseb, brilliant, it works.

During the tests of your script found an unrelated issue that has to do with VS Code (_feel free to split this to a new topic_):

On Win10 Julia 1.6.4’s REPL (all good):

```julia
julia> exp(rand(1000,1000))
julia> last_time
0.2909998893737793

```

while on Julia 1.7.0-rc3 VS Code REPL, there is a stack overflow for a much smaller matrix :

```julia
julia> exp(rand(50,50))
ERROR: StackOverflowError:

```

---

<div class="post-metadata">

**Author:** ![pfitzseb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pfitzseb/32/45566_2.png) [@pfitzseb](https://discourse.julialang.org/u/pfitzseb)\
**Post date:** [November 30, 2021, 1:09pm UTC](https://discourse.julialang.org/t/time-every-evaluation-in-repl/72275/8 "2021-11-30T13:09:52Z")

</div>

That looks like  
[https://github.com/julia-vscode/julia-vscode/issues/2543](https://github.com/julia-vscode/julia-vscode/issues/2543)  
[https://github.com/JuliaLang/julia/issues/43008](https://github.com/JuliaLang/julia/issues/43008)
