6

In MATLAB, there are a pair of functions tic and toc which can be used to start and stop a stopwatch timer. An example taken from link:

tic
A = rand(12000, 4400);
B = rand(12000, 4400);
toc
C = A'.*B';
toc

I am aware that there is a macro @time in Julia that has similar functionality.

julia> @time [sin(cos(i)) for i in 1:100000];
elapsed time: 0.00721026 seconds (800048 bytes allocated)

Is there a set of similar functions in Julia? The @time macro works well for timing statements that can be written in one or two lines. For longer portions of code, I would prefer to use tic-toc functions.

What I tried

When I googled "julia stopwatch", I found one useful link and four unrelated links.

  1. Introducing Julia/Metaprogramming - Wikibooks, open ... Meta-programming is when you write Julia code to process and modify Julia code. ... The @time macro inserts a "start the stopwatch" command at the beginning ...
  2. Our Invisible Stopwatch promo - YouTube Video for julia stopwatch
  3. Julia Larson on Twitter: "This #Mac OSX timer/stopwatch is ...
  4. Timing episodes of The French Chef with a stopwatch
  5. julia griffith | Oiselle Running Apparel for Women

I don't know why I hadn't thought of just trying tic() and toc().

I Like to Code
  • 7,101
  • 13
  • 38
  • 48
  • 5
    Julia has `tic()` and `toc()` too... However, please do read the Julia Performance Tips http://docs.julialang.org/en/latest/manual/performance-tips/#performance-tips ... The tl;dr is that you will be best off putting your computation inside of a function rather than doing everything at global scope, after which using `@time` will be simple. `@time` is also advantageous in that it reports memory allocation in addition to time. – Isaiah Norton Jul 20 '15 at 20:50
  • 2
    I don't see why this deserved a down-vote (maybe the extraneous material in the "What I tried section?). Overly harsh. +1 back to 0. Also, I strongly second Isaiah's comment. 90% of the "Why is my Julia slow" questions occur because the author is working in global scope, ie did not wrap code inside a function. – Colin T Bowers Jul 20 '15 at 23:53
  • @ColinTBowers I had actually received two downvotes. My guess is that cause was that the downvoters were upset that I did not just try `tic()` and `toc()`. I later added the "What I tried" section to explain why it was not obvious to me when I searched that the functions `tic()` and `toc()` actually exited in Julia. – I Like to Code Jul 21 '15 at 02:18
  • Understood. A "What I tried" section is usually a good idea, and making it explicit like you have is even better. Of course, sometimes people can get a bit silly. I remember when [this question](http://stackoverflow.com/questions/12701841/xkcd-style-graphs-in-matlab) was closed because it didn't initially contain a "what I have tried" section... – Colin T Bowers Jul 21 '15 at 03:17

3 Answers3

8

tic() and toc() have been deprecated as of https://github.com/JuliaLang/julia/commit/1b023388f49e13e7a42a899c12602d0fd5d60b0a

You can use @elapsed and @time for longer chunks by wrapping them in an environment, like so:

t = @elapsed begin
    ...
end

There is also TickTock.jl, which has reimplemented tic() and toc() and tick() and tock().

using TickTock
tick()
# Started timer at 2017-12-13T22:30:59.632
tock()
# 55.052638936 ms: 55 seconds, 52 milliseconds
Johan Larsson
  • 3,496
  • 18
  • 34
6

From a search of the Julia documentation

tic()

Set a timer to be read by the next call to toc() or toq(). The macro call @time expr can also be used to time evaluation.

IainDunning
  • 11,546
  • 28
  • 43
1

You can also use time() to get similar output. For example:

t = time()

# code block here
# ...
# ...

dt = time() - t
Shep Bryan
  • 567
  • 5
  • 13