Timing Julia execution

This is probably a silly question, but I’d like to time a sequence of commands. I know that I can use @time statement, but how do I time a sequence of statements?

statement 1
statement 2
statement n

So – how do I bracket the statements so that Julia times the total execution time?
[I dug up some MATLAB code from 1993 or so… at that time, MATLAB took 7 hours to solve the problem on a DECStation 3000… Today, MATLAB took ca. 9 seconds to solve the same problem on my desktop… I did a crude rewrite into Julia, and my guesstimate is ca. 5 seconds in Julia, but I’d like to know.]

You can time an entire block of statements like this:

@time begin

Or you can put your code into a function and time calling that function. Using functions will probably make your code faster and easier to use, so I’d recommend that anyway.

You also might want to check out BenchmarkTools.jl for more accurate benchmarking.


Thanks. I think I tried something similar in the past, but I must have done something wrong then…

The execution takes at least a handful of seconds, so I’d like to do it like you suggest first.

Write all relevant code in functions to avoid the global scope. Global scope is bad in Julia and will slow your program.

Next you have to run @time func() twice. The first time you run it, you are also capturing compile time. The second and any subsequeuent run should tell you the “true” runtime of your code.

Alternatively use @btime from the BenchMarkTools package.