Updated README, added equality test and pop benchmark

This commit is contained in:
Nocturn9x 2022-03-15 17:22:03 +01:00
parent 2fba62708f
commit 9ee6bdfb96
4 changed files with 67 additions and 2 deletions

View File

@ -92,4 +92,13 @@ queue.clearPop()
## Disclaimer ## Disclaimer
This is mostly a toy, there are no performance guarantees nor particular optimizations other than very obvious ones. With This is mostly a toy, there are no performance guarantees nor particular optimizations other than very obvious ones. With
that said, the collections _do_ work and are tested somewhat thoroughly (please report any bugs!) that said, the collections _do_ work and are tested somewhat thoroughly (please report any bugs!). The tests directory contains
some benchmarks as well as the test suite used to validate the behavior of the queues.
## Why? There's std/deques!
1. I was bored during my programming class
2. That only provides a deque based on `seq`s
3. The deque in that module is a value type
4. I was bored during my programming class

View File

@ -27,6 +27,7 @@ export linked.high
export pop export pop
export `[]` export `[]`
export `[]=` export `[]=`
export `==`
export pairs export pairs
export linked.`$` export linked.`$`
export insert export insert

View File

@ -170,7 +170,8 @@ proc clear*[T](self: LinkedDeque[T]) =
proc clearPop*[T](self: LinkedDeque[T]) = proc clearPop*[T](self: LinkedDeque[T]) =
## Clears the deque by repeatedly ## Clears the deque by repeatedly
## calling self.pop() in O(n) time ## calling self.pop() in O(1) time,
## slower than clear()
while self.len() > 0: while self.len() > 0:
discard self.pop() discard self.pop()

View File

@ -133,6 +133,17 @@ when isMainModule:
deque.clear() deque.clear()
doAssert deque.len() == 0 doAssert deque.len() == 0
doAssertRaises(IndexDefect, echo deque[0]) doAssertRaises(IndexDefect, echo deque[0])
echo "\t- Testing equality"
var
a = newLinkedDeque[int]()
b = newLinkedDeque[int]()
for i in countup(0, size - 1):
a.add(i)
# I could add to both a and b together
# but ¯\_(ツ)_/¯ who cares
b.extend(a)
doAssert a == b
echo &"Tests completed in {cpuTime() - testStart} seconds" echo &"Tests completed in {cpuTime() - testStart} seconds"
## End of tests, start of benchmark ## End of tests, start of benchmark
@ -144,6 +155,7 @@ when isMainModule:
var st: RunningStat var st: RunningStat
var benchStart = cpuTime() var benchStart = cpuTime()
var start = cpuTime() var start = cpuTime()
echo &" Benchmarking LinkedDeque.add()" echo &" Benchmarking LinkedDeque.add()"
for i in countup(0, benchSize - 1): for i in countup(0, benchSize - 1):
tmp = cpuTime() tmp = cpuTime()
@ -156,6 +168,7 @@ when isMainModule:
- max: {st.max} - max: {st.max}
- avg: {st.mean()} - avg: {st.mean()}
- stdev: {st.standardDeviation()}""" - stdev: {st.standardDeviation()}"""
st.clear() st.clear()
t = @[] t = @[]
start = cpuTime() start = cpuTime()
@ -171,6 +184,7 @@ when isMainModule:
- max: {st.max} - max: {st.max}
- avg: {st.mean()} - avg: {st.mean()}
- stdev: {st.standardDeviation()}""" - stdev: {st.standardDeviation()}"""
st.clear() st.clear()
t = @[] t = @[]
start = cpuTime() start = cpuTime()
@ -186,6 +200,7 @@ when isMainModule:
- max: {st.max} - max: {st.max}
- avg: {st.mean()} - avg: {st.mean()}
- stdev: {st.standardDeviation()}""" - stdev: {st.standardDeviation()}"""
st.clear() st.clear()
t = @[] t = @[]
start = cpuTime() start = cpuTime()
@ -201,6 +216,7 @@ when isMainModule:
- max: {st.max} - max: {st.max}
- avg: {st.mean()} - avg: {st.mean()}
- stdev: {st.standardDeviation()}""" - stdev: {st.standardDeviation()}"""
st.clear() st.clear()
t = @[] t = @[]
start = cpuTime() start = cpuTime()
@ -217,6 +233,7 @@ when isMainModule:
- max: {st.max} - max: {st.max}
- avg: {st.mean()} - avg: {st.mean()}
- stdev: {st.standardDeviation()}""" - stdev: {st.standardDeviation()}"""
st.clear() st.clear()
t = @[] t = @[]
start = cpuTime() start = cpuTime()
@ -232,6 +249,7 @@ when isMainModule:
- max: {st.max} - max: {st.max}
- avg: {st.mean()} - avg: {st.mean()}
- stdev: {st.standardDeviation()}""" - stdev: {st.standardDeviation()}"""
st.clear() st.clear()
t = @[] t = @[]
start = cpuTime() start = cpuTime()
@ -247,6 +265,7 @@ when isMainModule:
- max: {st.max} - max: {st.max}
- avg: {st.mean()} - avg: {st.mean()}
- stdev: {st.standardDeviation()}""" - stdev: {st.standardDeviation()}"""
st.clear() st.clear()
t = @[] t = @[]
start = cpuTime() start = cpuTime()
@ -262,8 +281,43 @@ when isMainModule:
- max: {st.max} - max: {st.max}
- avg: {st.mean()} - avg: {st.mean()}
- stdev: {st.standardDeviation()}""" - stdev: {st.standardDeviation()}"""
st.clear() st.clear()
t = @[] t = @[]
t = @[]
start = cpuTime()
echo &" Benchmarking LinkedDeque.pop(^1)"
for i in countup(0, size * 10 - 1):
tmp = cpuTime()
discard q.pop(^1)
t.add(cpuTime() - tmp)
st.push(t)
echo &"""
- Done in {cpuTime() - start} seconds. Results (in seconds):
- min: {st.min}
- max: {st.max}
- avg: {st.mean()}
- stdev: {st.standardDeviation()}"""
st.clear()
t = @[]
start = cpuTime()
echo &" Benchmarking seq.pop()"
for i in countup(0, size * 10 - 1):
tmp = cpuTime()
discard q2.pop()
t.add(cpuTime() - tmp)
st.push(t)
echo &"""
- Done in {cpuTime() - start} seconds. Results (in seconds):
- min: {st.min}
- max: {st.max}
- avg: {st.mean()}
- stdev: {st.standardDeviation()}"""
st.clear()
t = @[]
start = cpuTime()
q2 = @[] q2 = @[]
echo &"Total benchmark time: {cpuTime() - benchStart}" echo &"Total benchmark time: {cpuTime() - benchStart}"