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

Add StopWatch? See suggested implementation.

Open dataPulverizer opened this issue 6 years ago • 2 comments

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.

dataPulverizer avatar Feb 09 '18 09:02 dataPulverizer

If I understand correctly this is like tic and toq from Julia < 0.7, right?

ararslan avatar Feb 09 '18 19:02 ararslan

To be honest, I didn't know about tic and toq but I get the general idea yes.

dataPulverizer avatar Feb 09 '18 20:02 dataPulverizer