TickTock.jl icon indicating copy to clipboard operation
TickTock.jl copied to clipboard

tick tock timing (but it’s tick() tock() rather than tic() toc() ...)

TickTock

Build Status Code Coverage
Build Status

tick tock

This module provides simple timer functions:

  • tick() start a timer
  • tock() stop a timer, show total elapsed time
  • tok() stop a timer, return elapsed seconds
  • laptimer() continue timing, show total elapsed time of active timers
  • peektimer() continue timing, return elapsed seconds of most recent timer
  • @async alarm(h, m, s) set an alarm timer that goes off in h hours, m minutes, s seconds
  • alarmlist() see alarms

laptimer() and peektimer() functions show your current timing activity without stopping any active timers.

Don't use these for timing code execution!

Julia provides much better facilities for measuring performance, ranging from the @time and @elapsed macros to packages such as BenchmarkTools.jl. (And remember, don't time Julia code running in global scope!) The TimerOutputs.jl package provides tools for timing different sections of a program.

Suggestions for use

You can:

  • time how long a phone call takes without leaving the Julia REPL
julia> using TickTock
julia> tick()
[ Info:  started timer at 2017-12-13T22:30:59.632
julia> tock()
[ Info:  55.052638936s: 55 seconds, 52 milliseconds
  • see how long your cup of tea's been brewing:
julia> tick()
[ Info:  started timer at 2017-12-13T22:34:03.78
julia> laptimer()
[ Info:  72.625839832s: 1 minute, 12 seconds, 625 milliseconds
julia> laptimer()
[ Info:  266.053953749s: 4 minutes, 26 seconds, 53 milliseconds
julia> laptimer()
[ Info:  285.314459174s: 4 minutes, 45 seconds, 314 milliseconds
  • see how many seconds you held your breath for:
julia> tick()
[ Info:  started timer at 2017-12-12T09:17:45.504

julia> tok()
287.841546621
  • see how long your computer (and Julia session) has been running for:
julia> tick()
...go on holiday, then come back
julia> laptimer()
[ Info:   1.302200135485876e6s: 2 weeks, 1 day, 1 hour, 43 minutes, 20 seconds, 135 milliseconds
  • time a number of things:
julia> tick()
 started timer at: 2018-03-17T12:08:43.326
julia> tick()
 started timer at: 2018-03-17T12:14:03.077
julia> laptimer()
2    [ Info:           7.315769543s: 7 seconds, 315 milliseconds
1    [ Info:         327.074715234s: 5 minutes, 27 seconds, 74 milliseconds
  • set an alarm to wake up in 1m30s:
julia> using Dates

julia> @async alarm(now() + Dates.Minute(1) + Dates.Second(30))
  • execute an anonymous function when the alarm fires:
julia> @async alarm(now() + Dates.Minute(0) + Dates.Second(5),
           action = () -> run(`say "wake up"`)) # MacOS speech command
  • print lots of text when the alarm fires:
@async alarm(0, 0, 10, alarmname="Wham!", action=() -> println("wake me up, before you go! " ^ 100))
  • continuously monitor something every minute. For exaample, say you were using JuliaCon.jl to monitor what was currently happening in JuliaCon:
using JuliaCon, TickTock, Dates
JuliaCon.debugmode(true) # if JuliaCon isn't really running 
jca() = begin
            print("\033[2J") # clear the console
            println(now())
            JuliaCon.now()  
            @async alarm(0, 0, 20, alarmname="What's happening now in JuliaCon!", 
                action=() -> jca())
        end
jca()

(How do you stop this though? No idea...)

  • on MacOS you can have speech:
@async alarm(0, 0, 5, action = () -> run(`say "Your Earl Grey is ready; sir"`), alarmname="tea's up") 

TODO: Add Linux and Windows speech commands...

  • check alarms
julia> alarmlist()

start    | duration | finish     | name
13:22:37 | 00:01:30 | 13:24:07   | TickTock alarm

You should not use this package to:

  • measure performance of Julia code

  • do benchmarking of Julia code

You can hide the message generated by tick() using:

ENV["TICKTOCK_MESSAGES"] = false

History

Some of this code used to live in Julia Base in the tic(), toc(), and toq() functions (in base/util.jl). They were deprecated in GitHub issue 17046.