Compare commits

...

3 Commits

Author SHA1 Message Date
Nocturn9x 71fa891f9e Separated tests and benchmark 2022-03-16 13:42:30 +01:00
Nocturn9x f979af98d3 Added test for find 2022-03-16 13:39:32 +01:00
Nocturn9x a3bcbc36e3 Added find() procedure 2022-03-16 13:31:29 +01:00
4 changed files with 233 additions and 208 deletions

184
bench/linked.nim Normal file
View File

@ -0,0 +1,184 @@
import times
import stats
import random
import strformat
import ../src/nimdeque
when isMainModule:
const benchSize = 500000
echo &"\nRunning benchmarks with queue size of {benchSize} against a seq"
var q = newLinkedDeque[int]()
var q2: seq[int] = @[]
var t: seq[float] = @[]
var tmp: float
var st: RunningStat
var benchStart = cpuTime()
var start = cpuTime()
echo &" Benchmarking LinkedDeque.add()"
for i in countup(0, benchSize - 1):
tmp = cpuTime()
q.add(i)
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.add()"
for i in countup(0, benchSize - 1):
tmp = cpuTime()
q2.add(i)
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 LinkedDeque.pop(0)"
for i in countup(0, benchSize - 1):
tmp = cpuTime()
discard q.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()
echo &" Benchmarking seq.delete(0)"
for i in countup(0, benchSize - 1):
tmp = cpuTime()
q2.delete(0)
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 &" Benchmarking LinkedDeque.addLeft()"
for i in countup(0, benchSize - 1):
tmp = cpuTime()
q.addLeft(i)
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.insert(0)"
for i in countup(0, benchSize - 1):
tmp = cpuTime()
q2.insert(i, 0)
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 random access for LinkedDeque (10000 times)"
for i in countup(0, 10000):
tmp = cpuTime()
discard q[rand(benchSize - 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 random access for seq (10000 times)"
for i in countup(0, 10000):
tmp = cpuTime()
discard q2[rand(benchSize - 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 = @[]
t = @[]
start = cpuTime()
echo &" Benchmarking LinkedDeque.pop(^1)"
for i in countup(0, benchSize - 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, benchSize - 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 &"\nTotal benchmark time: {cpuTime() - benchStart}"

View File

@ -36,4 +36,5 @@ export reversedPairs
export clear
export clearPop
export extendLeft
export maxSize
export maxSize
export find

View File

@ -48,7 +48,7 @@ proc newDequeNode[T](val: T): DequeNode[T] =
result.val = val
proc newLinkedDeque*[T](maxSize: int = 0): LinkedDeque[T] =
proc newLinkedDeque*[T](maxSize: int = 0): LinkedDeque[T] {.raises: [ValueError].} =
## Initializes a new, empty
## LinkedDeque object with an
## optional size limit. A maxSize
@ -80,7 +80,7 @@ proc maxSize*[T](self: LinkedDeque[T]): int =
result = self.maxSize
proc getNode[T](self: LinkedDeque[T], i: int): DequeNode[T] =
proc getNode[T](self: LinkedDeque[T], i: int): DequeNode[T] {.raises: [IndexDefect, ValueError].} =
## Low level method for indexing and getting
## a node object back
if self.high() == -1:
@ -108,30 +108,30 @@ proc getNode[T](self: LinkedDeque[T], i: int): DequeNode[T] =
dec(pos)
proc `[]`*[T](self: LinkedDeque[T], i: int): T =
proc `[]`*[T](self: LinkedDeque[T], i: int): T {.raises: [IndexDefect, ValueError].} =
## Implements indexing into the queue
result = self.getNode(i).val
proc `[]=`*[T](self: LinkedDeque[T], i: int, val: T) =
proc `[]=`*[T](self: LinkedDeque[T], i: int, val: T) {.raises: [IndexDefect, ValueError].} =
## Sets element at position i
## to the given value
self[i].val = val
proc `[]`*[T](self: LinkedDeque[T], i: BackwardsIndex): T =
proc `[]`*[T](self: LinkedDeque[T], i: BackwardsIndex): T {.raises: [IndexDefect, ValueError].} =
## Implements indexing into the queue
## with backwards indeces
result = self[self.size - int(i)]
proc `[]=`*[T](self: LinkedDeque[T], i: BackwardsIndex, val: T) =
proc `[]=`*[T](self: LinkedDeque[T], i: BackwardsIndex, val: T) {.raises: [IndexDefect, ValueError].} =
## Sets element at backwards
## position i to the given value
self[self.size - int(i)] = val
proc pop*[T](self: LinkedDeque[T], pos: int = 0): T =
proc pop*[T](self: LinkedDeque[T], pos: int = 0): T {.raises: [IndexDefect, ValueError].} =
## Pops an element off the deque
## at the given index (default 0).
## The queue is optimized for popping
@ -155,7 +155,7 @@ proc pop*[T](self: LinkedDeque[T], pos: int = 0): T =
dec(self.size)
proc pop*[T](self: LinkedDeque[T], pos: BackwardsIndex): T =
proc pop*[T](self: LinkedDeque[T], pos: BackwardsIndex): T {.raises: [IndexDefect, ValueError].} =
## Same as self.pop but for backwards indeces
result = self.pop(self.size - int(pos))
@ -207,17 +207,16 @@ proc addLeft*[T](self: LinkedDeque[T], val: T) =
inc(self.size)
proc insert*[T](self: LinkedDeque[T], pos: int, val: T) =
proc insert*[T](self: LinkedDeque[T], pos: int, val: T) {.raises: [IndexDefect, ValueError].} =
## Inserts the given value at the given
## position. When pos equals 0, this proc is equivalent
## to addLeft. In all other cases, all items are
## "shifted" by 1 (shifted is in quotes because
## no shifting actually occurs, but the result is
## the same). The operation takes constant time at
## the ends, but the complexity grows to O(n) the closer
## the index gets to the middle of the deque. This
## proc raises an IndexDefect if the queue's max
## size is reached
## "shifted" by 1 (this is not actually what happens,
## but the result is analogous). The operation takes
## constant time at the ends, but the complexity grows
## to O(n) the closer the index gets to the middle of
## the deque. This proc raises an IndexDefect if the
## queue's max size is reached
if self.maxSize > 0 and self.size == self.maxSize:
raise newException(IndexDefect, &"LinkedDeque has reached its maximum size ({self.maxSize})")
if pos == 0:
@ -315,6 +314,16 @@ proc extendLeft*[T](self: LinkedDeque[T], other: seq[T]) =
self.addLeft(item)
proc find*[T](self: LinkedDeque[T], val: T): int =
## Returns the first occurrence
## of val in the queue. Returns -1
## if the item isn't found
for i, item in self:
if item == val:
return i
return -1
proc `$`*[T](self: LinkedDeque[T]): string =
## Returns a string representation
## of the deque

View File

@ -19,18 +19,18 @@ import ../src/nimdeque
when isMainModule:
const size = 10
const benchSize = 500000
const size = 5000
const benchSize = 50000
echo &"Running tests with queue of size {size}"
var deque = newLinkedDeque[int]()
var testStart = cpuTime()
echo &"\t- Checking add()"
echo &"\t- Testing add()"
for i in countup(0, size - 1, 1):
deque.add(i)
doAssert deque.len() == size
echo "\t- Checking iteration"
echo "\t- Testing iteration"
for i in countup(0, size - 1, 1):
doAssert deque[i] == i
for i, e in deque:
@ -42,17 +42,17 @@ when isMainModule:
for i, e in deque.reversedPairs():
doAssert size - i - 1 == e
echo "\t- Checking contains()"
echo "\t- Testing contains()"
for i in countup(0, size - 1, 1):
doAssert i in deque
doAssert 48574857 notin deque
doAssert -0xfffffff notin deque
echo "\t- Checking pop(0)"
echo "\t- Testing pop(0)"
doAssert deque.pop() == 0
doAssert deque.len() == size - 1
doAssert deque[0] == 1
echo "\t- Checking pop(^1)"
echo "\t- Testing pop(^1)"
doAssert deque.pop(deque.high()) == size - 1
doAssert deque.len() == size - 2
doAssert deque[deque.high()] == size - 2
@ -61,7 +61,7 @@ when isMainModule:
for i in countup(0, size - 3, 1):
doAssert deque[i] == i + 1
echo "\t- Checking addLeft()"
echo "\t- Testing addLeft()"
deque.addLeft(0)
doAssert deque.len() == size - 1
doAssert deque[0] == 0
@ -70,21 +70,21 @@ when isMainModule:
for i in countup(0, size - 2, 1):
doAssert deque[i] == i
echo "\t- Checking insert(3)"
echo "\t- Testing insert(3)"
var oldLen = deque.len()
deque.insert(3, 69420)
doAssert oldLen + 1 == deque.len()
doAssert deque.pop(3) == 69420
doAssert deque.len() == oldLen
echo &"\t- Checking insert({size - 2})"
echo &"\t- Testing insert({size - 2})"
oldLen = deque.len()
deque.insert(size - 2, 0x42362)
doAssert oldLen + 1 == deque.len()
doAssert deque.pop(size - 2) == 0x42362
doAssert deque.len() == oldLen
echo &"\t- Checking insert({size div 2})"
echo &"\t- Testing insert({size div 2})"
oldLen = deque.len()
deque.insert(size div 2, 0xf7102)
doAssert oldLen + 1 == deque.len()
@ -93,20 +93,20 @@ when isMainModule:
randomize()
let idx = rand(size - 1)
echo &"\t- Checking insert({idx})"
echo &"\t- Testing insert({idx})"
oldLen = deque.len()
deque.insert(idx, 0xff)
doAssert oldLen + 1 == deque.len()
doAssert deque.pop(idx) == 0xff
doAssert deque.len() == oldLen
echo "\t- Checking backwards indeces"
echo "\t- Testing backwards indeces"
for i in countdown(deque.high(), 1):
doAssert deque[^i] == deque[deque.len() - i]
deque.add(deque.pop(^1))
doAssert deque[deque.high()] == deque[^1]
echo &"\t- Checking queue with maxSize {size div 2}"
echo &"\t- Testing queue with maxSize {size div 2}"
var queue = newLinkedDeque[int](size div 2)
for i in countup(0, (size div 2) - 1):
queue.add(i)
@ -150,181 +150,12 @@ when isMainModule:
# but ¯\_(ツ)_/¯ who cares
b.extend(a)
doAssert a == b
echo &"Tests completed in {cpuTime() - testStart} seconds"
## End of tests, start of benchmark
echo &"\nRunning benchmarks with queue size of {benchSize} against a seq"
var q = newLinkedDeque[int]()
var q2: seq[int] = @[]
var t: seq[float] = @[]
var tmp: float
var st: RunningStat
var benchStart = cpuTime()
var start = cpuTime()
echo &" Benchmarking LinkedDeque.add()"
for i in countup(0, benchSize - 1):
tmp = cpuTime()
q.add(i)
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.add()"
for i in countup(0, benchSize - 1):
tmp = cpuTime()
q2.add(i)
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 LinkedDeque.pop(0)"
for i in countup(0, size * 10 - 1):
tmp = cpuTime()
discard q.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()
echo &" Benchmarking seq.delete(0)"
for i in countup(0, size * 10 - 1):
tmp = cpuTime()
q2.delete(0)
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 &" Benchmarking LinkedDeque.addLeft()"
for i in countup(0, benchSize - 1):
tmp = cpuTime()
q.addLeft(i)
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.insert(0)"
for i in countup(0, benchSize - 1):
tmp = cpuTime()
q2.insert(i, 0)
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 random access for LinkedDeque (10000 times)"
for i in countup(0, 10000):
tmp = cpuTime()
discard q[rand(benchSize - 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 random access for seq (10000 times)"
for i in countup(0, 10000):
tmp = cpuTime()
discard q2[rand(benchSize - 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 = @[]
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}"
echo &"Total execution time: {cpuTime() - testStart}"
echo "\t- Testing find()"
for i in countup(0, size - 1):
deque.add(i)
doAssert deque.find(size - 1) == size - 1
doAssert deque.find(0) == 0
doAssert deque.find(size) == -1
doAssert deque.find(size div 2) == size div 2
echo &"Tests completed in {cpuTime() - testStart} seconds"