BenchmarkTools.jl
BenchmarkTools.jl copied to clipboard
Add StopWatch? See suggested implementation.
Add simple stopwatch, see below for basic suggested implementation:
mutable struct StopWatch
t1::Float64
t2::Float64
StopWatch() = new(NaN, NaN)
end
function start!(sw::StopWatch)::Nothing
sw.t1 = time()
Nothing()
end
function stop!(sw::StopWatch)::Nothing
sw.t2 = time()
Nothing()
end
function reset!(sw::StopWatch)::Nothing
sw.t2 = sw.t1 = NaN
Nothing()
end
peek(sw::StopWatch) = isnan(sw.t1) ? throw("StopWatch ($sw) has not been started.") : isnan(sw.t2) ? time() - sw.t1 : sw.t2 - sw.t1
# duration(sw::StopWatch) = sw.t2 - sw.t1
# Demo
cl = StopWatch() # peek(cl) # error
start!(cl); sleep(2); peek(cl)
sleep(1); stop!(cl); peek(cl); reset!(cl)
Because sometimes you just want a stop watch.
If I understand correctly this is like tic
and toq
from Julia < 0.7, right?
To be honest, I didn't know about tic
and toq
but I get the general idea yes.