Skip to content

Commit 78f54ec

Browse files
committed
add lap and peek
1 parent 758b258 commit 78f54ec

File tree

3 files changed

+79
-25
lines changed

3 files changed

+79
-25
lines changed

README.md

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,64 @@
11
# TickTock
22

3-
This module provides `tick()`, `tock()`, and `tok()` functions.
4-
5-
They're similar to the `tic()`, `toc()`, and `toq()` functions that you might find in MATLAB and
6-
similar software.
3+
This module provides `tick()`, `tock()`, and `tok()` functions. They're similar to the `tic()`, `toc()`, and `toq()` functions that you might find in MATLAB and
4+
similar software. There are also `lap()` and `peek()` functions that reveal the state of the current timer without stopping it.
75

86
**Don't use these for timing code execution!** Julia provides much better facilities for
97
measuring performance, ranging from the `@time` and `@elapsed` macros to packages such as [BenchmarkTools.jl](https://github.com/JuliaCI/BenchmarkTools.jl). (And remember, don't
10-
time Julia code running in global scope!)
8+
time Julia code running in global scope!) The [TimerOutputs.jl](https://github.com/KristofferC/TimerOutputs.jl) package provides tools for timing different sections of a program.
119

12-
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](https://github.com/JuliaLang/julia/issues/17046).
10+
## Functions
1311

14-
The [TimerOutputs.jl](https://github.com/KristofferC/TimerOutputs.jl) package provides tools for timing different sections of a program.
12+
- `tick()` start counting
13+
- `tock()` stop counting show total elapsed time
14+
- `tok()` stop counting return seconds
15+
- `peek()` continue counting, return elapsed seconds
16+
- `lap()` continue counting, show total elapsed time
1517

16-
## Example
18+
## Suggestions for use
1719

18-
```julia
19-
julia-0.6> using TickTock
20+
You can:
2021

21-
julia-0.6> tick()
22-
INFO: Started timer at 2017-12-12T09:33:00.363.
22+
- time how long a phone call takes without leaving the Julia REPL
2323

24+
```
25+
julia-0.6> using TickTock
26+
julia-0.6> tick()
27+
INFO: Started timer: 2017-12-13T22:30:59.632.
2428
julia-0.6> tock()
25-
INFO: Time taken: 149.427977832
26-
INFO: 2 minutes, 29 seconds, 427 milliseconds
29+
INFO: 55.052638936 ms: 55 seconds, 52 milliseconds
2730
```
2831

29-
To return the elapsed time in seconds, use `tok()`:
32+
- see whether your cup of tea's brewed for the right length of time:
3033

31-
```julia
34+
julia-0.6> tick()
35+
INFO: Started timer: 2017-12-13T22:34:03.78.
36+
julia-0.6> lap()
37+
INFO: 72.625839832 ms: 1 minute, 12 seconds, 625 milliseconds
38+
julia-0.6> lap()
39+
INFO: 266.053953749 ms: 4 minutes, 26 seconds, 53 milliseconds
40+
julia-0.6> lap()
41+
INFO: 285.314459174 ms: 4 minutes, 45 seconds, 314 milliseconds
42+
```
43+
44+
- see how many seconds you held your breath for:
45+
46+
```
3247
julia-0.6> tick()
3348
INFO: Started timer at 2017-12-12T09:17:45.504.
3449

3550
julia-0.6> tok()
3651
287.841546621
3752
```
3853
54+
You should not:
55+
56+
- measure performance of Julia code
57+
58+
## Acknowledgements
59+
60+
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](https://github.com/JuliaLang/julia/issues/17046).
61+
3962
[![Build Status](https://travis-ci.org/cormullion/TickTock.jl.svg?branch=master)](https://travis-ci.org/cormullion/TickTock.jl)
4063
4164
[![Coverage Status](https://coveralls.io/repos/cormullion/TickTock.jl/badge.svg?branch=master&service=github)](https://coveralls.io/github/cormullion/TickTock.jl?branch=master)

src/TickTock.jl

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This code used to live in Julia Base in the `tic()`, `toc()`, and `toq()` functi
1313

1414
module TickTock
1515

16-
export tick, tock, tok
16+
export tick, tock, tok, peek, lap
1717

1818
"""
1919
tick()
@@ -23,36 +23,63 @@ Start counting.
2323
function tick()
2424
t0 = time_ns()
2525
task_local_storage(:TIMERS, (t0, get(task_local_storage(), :TIMERS, ())))
26-
info("Started timer at $(now()).")
26+
info("Started timer: $(now()).")
2727
end
2828

2929
"""
30-
tok()
30+
peek()
3131
32-
Return the time since the previous `tick()`, in seconds, and then stop counting.
32+
Return the seconds counted by the current timer, without stopping it.
3333
"""
34-
function tok()
34+
function peek()
3535
t1 = time_ns()
3636
timers = get(task_local_storage(), :TIMERS, ())
3737
if timers === ()
3838
error("You must first use `tick()`.")
3939
end
4040
t0 = timers[1]::UInt64
41+
return (t1 - t0)/1e9
42+
end
43+
44+
"""
45+
tok()
46+
47+
Return the seconds since the previous `tick()` then stop counting.
48+
"""
49+
function tok()
50+
timers = get(task_local_storage(), :TIMERS, ())
51+
if timers === ()
52+
error("You must first use `tick()`.")
53+
end
54+
t = peek()
4155
task_local_storage(:TIMERS, timers[2])
42-
return (t1-t0)/1e9 # seconds
56+
return t
4357
end
4458

4559
"""
4660
tock()
4761
4862
Print the elapsed time, in canonical form, since the previous `tick()`,
49-
and then stop counting.
63+
then stop counting.
5064
"""
5165
function tock()
5266
t = tok()
5367
canondc = Dates.canonicalize(Dates.CompoundPeriod(Dates.Second(floor(t)), Dates.Millisecond(floor((t-floor(t)) * 1000))))
54-
info("Time taken: $t")
55-
info(" $canondc")
68+
info("$(t)ms: ($canondc)")
69+
end
70+
71+
"""
72+
lap()
73+
74+
Print the current elapsed time, in canonical form, since the previous `tick()`,
75+
and continue counting.
76+
"""
77+
function lap()
78+
t = peek()
79+
canondc = Dates.canonicalize(Dates.CompoundPeriod(Dates.Second(floor(t)), Dates.Millisecond(floor((t-floor(t)) * 1000))))
80+
info("$(t)ms: ($canondc)")
5681
end
5782

83+
84+
5885
end # module

test/runtests.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,8 @@ using Base.Test
33

44
tick()
55
sleep(1)
6+
@test typeof(peek()) == Float64
7+
@test typeof(lap()) == Void
68
@test tok() > 1.0
9+
tick()
10+
@test typeof(tock()) == Void

0 commit comments

Comments
 (0)