Kjetil Sonerud
Kjetil Sonerud

Reputation: 275

How to avoid toc() printing the time elapsed in Julia?

This might be a really stupid question, but I'll go ahead anyway. I'm trying to use Julia's tic() and toc() inside a loop in order to figure out some timing issues. A dummy example is shown below:

elapsedTime = zeros(3);

for i = 1:3
    tic();
    pause(i)
    ElapsedTime[i] = toc();
end 

The goal is to store the elapsed time intervals in the array ElapsedTime. The issue is that toc() seems to a) print the elapsed time to screen and b) store it as wanted.

Is there some simple trick to avoid a), ie. that toc() prints the result? It's not a huge problem, just annoying if the number of iterations is large.

Any help will be greatly appreciated!

Upvotes: 12

Views: 3167

Answers (3)

Dan Getz
Dan Getz

Reputation: 18217

there is another version of toc() called toq() which doesn't print a thing and returns the elapsed time.

Upvotes: 19

one-more-minute
one-more-minute

Reputation: 1406

You'll want the @elapsed macro.

elapsedTime = zeros(3);

for i = 1:3
  elapsedTime[i] = @elapsed sleep(i)
end

Upvotes: 9

Mr Alpha
Mr Alpha

Reputation: 1843

I suppose you could take the time manually and store it in the array>

elapsedTime = zeros(3);

for i = 1:3
    t1 = time_ns()
    rand(10000000)
    t2 = time_ns()
    elapsedTime[i] = (t2 - t1)/1.0e9
end 

Upvotes: 1

Related Questions