Fam
Fam

Reputation: 533

How to measure execution time of each loop step in Julia?

I have a custom function and I would like to evaluate its media performance. For this, I would like to loop by executing my function a certain number of times. I want to do this because I see that the runtime is very unstable.

A typical execution measuring the execution time would look something like this

@time my_function()

or

@time begin
my_function()
end

In this particular case, I only visualize the execution time, but I don't know how to register the execution time to allocate the time for each iteration in a vector. For example, I would like something like this

vector_time = 1:100
for i in 1:100
    @time my_function()
    vector_time[i] = get_time_i # register the time of the i-th iteration 
end

Upvotes: 1

Views: 1904

Answers (2)

rjeyaram
rjeyaram

Reputation: 91

If you are timing multiple sections of your code at once, you can also use TimerOutputs.jl, which I've found to be very convenient. It automatically computes average runtimes and % total runtime.

Upvotes: 1

Jun Tian
Jun Tian

Reputation: 1390

Try @elapsed instead to get the execution time in seconds.

Upvotes: 2

Related Questions