diff --git a/README.md b/README.md index 8f97517..bca02e5 100644 --- a/README.md +++ b/README.md @@ -92,4 +92,13 @@ queue.clearPop() ## Disclaimer 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 + diff --git a/src/nimdeque.nim b/src/nimdeque.nim index 05f0b0d..1a40577 100644 --- a/src/nimdeque.nim +++ b/src/nimdeque.nim @@ -27,6 +27,7 @@ export linked.high export pop export `[]` export `[]=` +export `==` export pairs export linked.`$` export insert diff --git a/src/private/queues/linked.nim b/src/private/queues/linked.nim index 62b5651..f2072b3 100644 --- a/src/private/queues/linked.nim +++ b/src/private/queues/linked.nim @@ -170,7 +170,8 @@ proc clear*[T](self: LinkedDeque[T]) = proc clearPop*[T](self: LinkedDeque[T]) = ## 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: discard self.pop() diff --git a/tests/linked.nim b/tests/linked.nim index a3e4925..1663e31 100644 --- a/tests/linked.nim +++ b/tests/linked.nim @@ -133,6 +133,17 @@ when isMainModule: deque.clear() doAssert deque.len() == 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" ## End of tests, start of benchmark @@ -144,6 +155,7 @@ when isMainModule: var st: RunningStat var benchStart = cpuTime() var start = cpuTime() + echo &" Benchmarking LinkedDeque.add()" for i in countup(0, benchSize - 1): tmp = cpuTime() @@ -156,6 +168,7 @@ when isMainModule: - max: {st.max} - avg: {st.mean()} - stdev: {st.standardDeviation()}""" + st.clear() t = @[] start = cpuTime() @@ -171,6 +184,7 @@ when isMainModule: - max: {st.max} - avg: {st.mean()} - stdev: {st.standardDeviation()}""" + st.clear() t = @[] start = cpuTime() @@ -186,6 +200,7 @@ when isMainModule: - max: {st.max} - avg: {st.mean()} - stdev: {st.standardDeviation()}""" + st.clear() t = @[] start = cpuTime() @@ -201,6 +216,7 @@ when isMainModule: - max: {st.max} - avg: {st.mean()} - stdev: {st.standardDeviation()}""" + st.clear() t = @[] start = cpuTime() @@ -217,6 +233,7 @@ when isMainModule: - max: {st.max} - avg: {st.mean()} - stdev: {st.standardDeviation()}""" + st.clear() t = @[] start = cpuTime() @@ -232,6 +249,7 @@ when isMainModule: - max: {st.max} - avg: {st.mean()} - stdev: {st.standardDeviation()}""" + st.clear() t = @[] start = cpuTime() @@ -247,6 +265,7 @@ when isMainModule: - max: {st.max} - avg: {st.mean()} - stdev: {st.standardDeviation()}""" + st.clear() t = @[] start = cpuTime() @@ -262,8 +281,43 @@ when isMainModule: - max: {st.max} - avg: {st.mean()} - stdev: {st.standardDeviation()}""" + st.clear() 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 = @[] echo &"Total benchmark time: {cpuTime() - benchStart}"