# Capturing the output of \`@time\` as a String

**URL:** https://discourse.julialang.org/t/capturing-the-output-of-time-as-a-string/80524
**Category:** General Usage
**Tags:** question, time, io
**Created:** [May 5, 2022, 5:59am UTC](https://discourse.julialang.org/t/capturing-the-output-of-time-as-a-string/80524 "2022-05-05T05:59:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)
#### Post date: [May 5, 2022, 5:59am UTC](https://discourse.julialang.org/t/capturing-the-output-of-time-as-a-string/80524/1 "2022-05-05T05:59:49Z")

</div>

I would like to print the output of a `@time` in a production environment. To avoid cluttering up the logs, I try to keep a bit of discipline with the logging so everything should use a log level. Now my question is, how can I print `@time` with a info log-level? The following doesn’t work:

```julia
julia> @info @time sum([1, 2])
  0.000001 seconds (1 allocation: 80 bytes)
[ Info: 3

```

because `@time` prints to `stdout`. At the same time, `capture_stdout` requires me to write to a file ([https://github.com/JuliaLang/julia/issues/12711](https://github.com/JuliaLang/julia/issues/12711))?

EDIT: There is [`IOCapture.jl`](https://github.com/JuliaDocs/IOCapture.jl/). It is a bit unfortunate to be required to install that. Maybe there is another workaround for this case.

---

<div class="post-metadata">

### Author: ![jedforrest](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jedforrest/32/37502_2.png) [@jedforrest](https://discourse.julialang.org/u/jedforrest)
#### Post date: [May 5, 2022, 8:00am UTC](https://discourse.julialang.org/t/capturing-the-output-of-time-as-a-string/80524/2 "2022-05-05T08:00:12Z")

</div>

Does `@timed` give you what you want? This returns the output of `@time` rather than printing to `stdout`.

```julia
julia> stats = @timed rand(10^6);

julia> stats.time
0.006634834

```

---

<div class="post-metadata">

### Author: ![jacobusmmsmit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jacobusmmsmit/32/217669_2.png) [@jacobusmmsmit](https://discourse.julialang.org/u/jacobusmmsmit)
#### Post date: [May 5, 2022, 11:26am UTC](https://discourse.julialang.org/t/capturing-the-output-of-time-as-a-string/80524/3 "2022-05-05T11:26:29Z")

</div>

Perhaps you can just use `@elapsed` which, rather than returning the proper return value of the expression, returns the time elapsed to the call.  
As far as I can tell it’s essentially `@timed` but only returns the `time` variable of the returned object i.e.

```julia
a = @elapsed f(x)
b = @timed f(x)
b.time == a # once both have been compiled

```
