Commit 661cd926 authored by Benjamin Summerton's avatar Benjamin Summerton

Almost there...

parent fce34334
......@@ -24,7 +24,8 @@ proc secs*(nsecs: int64): float {.inline.}
Stopwatch* = object
running: bool
startTicks: Ticks
startTicks: Ticks
recordLaps: bool
laps: seq[Ticks]
total: Nanos
......@@ -38,7 +39,7 @@ proc reset*(sw: var Stopwatch) {.inline.}
proc restart*(sw: var Stopwatch) {.inline.}
# Lap functions
proc isRecordingLaps(sw: var Stopwatch;): bool {.inline.}
proc isRecordingLaps*(sw: var Stopwatch;): bool {.inline.}
proc numLaps*(sw: var Stopwatch; incCur: bool = false): int {.inline.}
proc lap*(sw: var Stopwatch; num: int; incCur: bool = false): int64 {.inline.}
proc laps*(sw: var Stopwatch; incCur: bool = false): seq[int64] {.inline.}
......@@ -131,6 +132,7 @@ proc stopwatch*(enableLapping:bool): Stopwatch =
result = Stopwatch(
running: false,
startTicks: 0.Ticks,
recordLaps: enableLapping,
laps: if enableLapping: @[] else: nil,
total: 0
......@@ -142,6 +144,7 @@ proc clone*(sw: var Stopwatch): Stopwatch =
result = Stopwatch(
running: sw.running,
startTicks: sw.startTicks,
recordLaps: sw.recordLaps,
laps: sw.laps,
......@@ -210,8 +213,8 @@ proc restart*(sw: var Stopwatch) =
## Checks to see if a stopwatch it recording laps or not. Returns true if so,
## false otherwise
proc isRecordingLaps(sw: var Stopwatch;): bool =
return sw.laps.addr != nil
proc isRecordingLaps*(sw: var Stopwatch;): bool =
return sw.recordLaps
## Returns the number of laps the Stopwatch has recorded so far. If `incCur` is
## set to `true`, it will include the current lap in the count. By default it
import stopwatch
import parseopt2
from os import sleep
from sequtils import map
var sw = stopwatch()
recordLaps = true
# Test with laps or no laps via an argument
for kind, key, val in getopt():
if (kind == cmdArgument) and (key == "nolap"):
recordLaps = false
# Print a message
echo "Testing with laps ", (if recordLaps: "on." else: "off.")
# Init the stopwatch and go
sw = stopwatch(recordLaps)
echo "laps=", sw.laps(true)
echo "total=", sw.totalSecs
......@@ -50,14 +67,14 @@ var sw2 = sw.clone()
echo "lapCount=", sw.numLaps
echo "L1=", sw.lap(0).secs
echo "L2=", sw.laps[1].secs
echo "L2=", if sw.isRecordingLaps: sw.laps()[1].secs else: 0
echo "all laps=", sw.laps
echo "lapCount=", sw.numLaps
echo "----"
echo "lapCount=", sw2.numLaps
var lapsSecs = int64): float = secs(x))
var lapsSecs = if sw.isRecordingLaps: sw2.laps().map(proc(x: int64): float = secs(x)) else: @[]
echo "all laps(s)=", lapsSecs
echo ""
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment