Time needed to perform a complete loop

I want to measure the total amount of time nedded to perform a complete loop. I tried

@time for i in 1:10000
...
...
end

and it returned .988083 seconds (57.58 M allocations: 39.867 GiB, 0.14% gc time) when the code ran about 15 minutes to complete the loop and not 0.988083 seconds as indicated. How can I measure time correctly?

Tell us whatā€™s in the ...? Normally what you did should work fine.

1 Like

The loop calls a lot of functions I created outside of the loop and a dataset I have :frowning: :frowning: Since the code with the functions is large, I thought that putting just the command Iā€™m using to measure time would be enough to know if Iā€™m using the right command or placing wrongly @time :frowning:

It should all be fine unless you have @asyncs or threads or whatever. Itā€™s easy to convince yourself that this should work fine:

julia> @time for i = 1:3
           sleep(1)
       end
  3.006142 seconds (15 allocations: 432 bytes)

So there has to be something else going on.

2 Likes

Ah thanks @tim.holy! Will check my code then

@time often seems to miss compilation time, at least in generated functions:

julia> @generated function slow_to_compile(x)
           sleep(10)
           :(x)
       end
slow_to_compile (generic function with 1 method)

julia> t0 = time(); @time for i āˆˆ 1:3
           sleep(slow_to_compile(i))
       end; time() - t0
  6.009163 seconds (15 allocations: 432 bytes)
16.032392978668213

julia> versioninfo()
Julia Version 1.6.0-DEV.355
Commit 955beb6a91* (2020-07-02 10:28 UTC)
2 Likes

This is a big of a tangent, but you shouldnā€™t be doing heavy computations in a globally scoped for loop as this will be quite bad for performance and memory usage. Consider using a let block or a function (and avoid global variables).

1 Like

Ok :slight_smile: Thanks for your help :slight_smile: I.m trying to see if I can measure the exact time with another command :slight_smile:

Just out of curiosity, are you using Juno? I saw it sometimes in Juno, that output of @time is being partially ā€œeatenā€ in console, I guess itā€™s some weird bug.

https://github.com/JuliaLang/julia/blob/66f41c58683d68b43ad0a2a9d8d605de72ca0de9/base/timing.jl#L171

1 Like

Yes Iā€™m using Juno, indeed!