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

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
  3.006142 seconds (15 allocations: 432 bytes)

So there has to be something else going on.


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)
slow_to_compile (generic function with 1 method)

julia> t0 = time(); @time for i ∈ 1:3
       end; time() - t0
  6.009163 seconds (15 allocations: 432 bytes)

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

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.

1 Like

Yes I’m using Juno, indeed!