# Redirect \`@time\` output into a string

**URL:** <https://discourse.julialang.org/t/redirect-time-output-into-a-string/124872>\
**Category:** General Usage\
**Tags:** question, input-output, io\
**Created:** [January 17, 2025, 11:59am UTC](https://discourse.julialang.org/t/redirect-time-output-into-a-string/124872 "2025-01-17T11:59:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex180500](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex180500/32/44358_2.png) [@alex180500](https://discourse.julialang.org/u/alex180500)\
**Post date:** [January 17, 2025, 11:59am UTC](https://discourse.julialang.org/t/redirect-time-output-into-a-string/124872/1 "2025-01-17T11:59:24Z")

</div>

Hello! working in a for loop I want to time a long computation with @time and I want to output the stdout into a string (that i’m using later in the for loop).

```julia
for _ in 1:num_loops
    @time myvar = longcomputation()
end

```

I want to keep the `@time` output inside a string variable and I also want to keep `myvar` as it get used later. How can I achieve this?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [January 17, 2025, 12:21pm UTC](https://discourse.julialang.org/t/redirect-time-output-into-a-string/124872/2 "2025-01-17T12:21:04Z")

</div>

```julia

longcomputation() = (v = rand(); sleep(1e-5*rand()); v)

function run_loop(num_loops)
    times = []
    vals = Float64[]
    for _ in 1:num_loops
        t = @timed myvar = longcomputation()
        push!(times, t)
        push!(vals, myvar)
    end
    return times, vals
end

times, vals = run_loop(1000)

```

maybe. It won’t give you the exact `@time` output but it will have the data.

If you want to dig into some internals you can convert it to a string via:

```julia
function timed_to_str(ret)
    sprint() do io
        Base.time_print(io, ret.time*1e9, ret.gcstats.allocd, ret.gcstats.total_time, Base.gc_alloc_count(ret.gcstats), ret.lock_conflicts, ret.compile_time*1e9, ret.recompile_time*1e9, true)
    end
end

```

```julia
julia> map(timed_to_str, times)
1000-element Vector{String}:
 " 0.002077 seconds (4 allocations: 112 bytes)\n"
 " 0.002110 seconds (4 allocations: 112 bytes)\n"
 " 0.002121 seconds (4 allocations: 112 bytes)\n"
 " 0.002097 seconds (4 allocations: 112 bytes)\n"
 " 0.002075 seconds (4 allocations: 112 bytes)\n"
 " 0.002095 seconds (4 allocations: 112 bytes)\n"
 " 0.002107 seconds (4 allocations: 112 bytes)\n"
 " 0.002098 seconds (4 allocations: 112 bytes)\n"
 " 0.002118 seconds (4 allocations: 112 bytes)\n"
 " 0.002105 seconds (4 allocations: 112 bytes)\n"

```

I found this by looking at what `@edit @time 1+1` does.

---

<div class="post-metadata">

**Author:** ![alex180500](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alex180500/32/44358_2.png) [@alex180500](https://discourse.julialang.org/u/alex180500)\
**Post date:** [January 17, 2025, 1:47pm UTC](https://discourse.julialang.org/t/redirect-time-output-into-a-string/124872/3 "2025-01-17T13:47:53Z")

</div>

Amazing! Thank you a lot, yes I tried to replicate from timed but I was not able to figure it out.
