Just wanted to add to what @treeform mentioned, key distribution matters a lot
when deleting with tables. Check out the timings of sequential keys vs a more
distributed table access.
import tables, times, random, sugar
template bench(code: untyped) =
let t1 = cpuTime()
code
let t2 = cpuTime()
echo "Line ", instantiationInfo().line, " took ", t2 - t1
var t: Table[int, int]
const tests = 100_000
bench:
for i in 0 ..< tests:
t.add i, 1
bench:
for i in 0 ..< tests:
t.del i
let randomKeys = collect(newSeqOfCap(tests)):
for i in 0 ..< tests:
rand int.high
bench:
for i in 0 ..< tests:
t[randomKeys[i]] = 1
bench:
for i in 0 ..< tests:
t.del randomKeys[i]
Run
I get the following output with -d:danger -d:release:
* Line 12 took 0.003
* Line 16 took 7.209
* Line 24 took 0.00199999999999978
* Line 28 took 0.003000000000000114
In this example, deleting with sequential keys is 2400 times slower than keys
with a more random distribution.