Skip to content

Instantly share code, notes, and snippets.

@GiuseppeChillemi
Last active September 1, 2023 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GiuseppeChillemi/78e5bb5e89e23885149add5fb3a1dea4 to your computer and use it in GitHub Desktop.
Save GiuseppeChillemi/78e5bb5e89e23885149add5fb3a1dea4 to your computer and use it in GitHub Desktop.
Testing key-value speeds
Red [
Title: "Speed test key val"
purpose: "Evaluate which method and structures are better for key-val selection"
note: "It has some bugs, don't use it but speedtest-key-val2."
]
recycle/off
;=== Setup
test: copy []
top: 1000000
top2: 1000
search-for: to-word rejoin ["s" top + 1]
;=== Preparing blocks
repeat idx top [append test reduce [to-word rejoin ["s" idx] "here"]]
append test reduce [search-for "I am"]
bhtest: make hash! test
mtest: make map! test
bobtest: copy []
foreach [id val] test [
append/only bobtest reduce [id val]
]
hbobtest: make hash! []
foreach [id val] test [
append/only hbobtest copy [] reduce [id val]
]
hhbobtest: make hash! []
foreach [id val] test [
append/only hhbobtest make hash! reduce [id val]
]
obobtest: copy []
foreach [id val] test [
append/only obobtest make object! reduce [to-set-word id val]
]
hobobtest: make hash! []
foreach [id val] test [
append/only hobobtest make object! reduce [to-set-word id val]
]
;probe copy/part bobtest 10 halt
;=== Looping
;------------------------------------------------------
print dt [Prin " SELECT Block of key-value: " loop top2 [select/skip test search-for 2]]
print dt [Prin " PATH Block of key-value: " loop top2 [test/:search-for]]
print dt [Prin " FOREACH Block of key-value: " loop top2 [foreach [k v] test [if k = :search-for [break]]]]
print dt [Prin " PATH IDX Block of key-value: " loop top2 [
ln: length? test
idx: 1
until [idx < ln] [
k: test/:idx
idx: idx + 2
if k = :search-for [break]
]
]
]
;------------------------------------------------------
print dt [Prin " SELECT hashed-block of key-value: " loop top2 [select/skip bhtest search-for 2]]
print dt [Prin " PATH hashed-block of key-value: " loop top2 [bhtest/:search-for]]
print dt [Prin " FOREACH hashed-Block of key-value: " loop top2 [foreach [k v] bhtest [if k = :search-for [break]]]]
print dt [Prin " PATH IDX hashed-Block of key-value: " loop top2 [
ln: length? test
idx: 1
until [idx < ln] [
k: bhtest/:idx
idx: idx + 2
if k = :search-for [break]
]
]
]
;------------------------------------------------------
print dt [Prin " SELECT map of key-value: " loop top2 [select mtest search-for]]
print dt [Prin " PATH map of key-value: " loop top2 [mtest/:search-for]]
;------------------------------------------------------
print dt [Prin " SELECT block + block of key-value: " loop top2 [
bobtest: head bobtest
forall bobtest [if select/skip bobtest/1 search-for 2 [break]]
]
]
print dt [Prin " PATH block + block of key-value: " loop top2 [
bobtest: head bobtest
forall bobtest [if bobtest/1/:search-for [break]]
]
]
print dt [Prin "PATH IDX block + Block of key-value: " loop top2 [
ln: length? test
idx: 1
idx2: 2
until [idx < ln] [
k: bobtest/:idx/:idx2
idx: idx + 1
if k = :search-for [break]
]
]
]
;--------------------------------------------------------
print dt [Prin " SELECT hash-blk + block of key-val: " loop top2 [
hbobtest: head hbobtest
forall hbobtest [if select/skip hbobtest/1 search-for 2 [break]]
]
]
print dt [Prin " PATH hash-blk + block of key-val: " loop top2 [
hbobtest: head hbobtest
forall hbobtest [if hbobtest/1/:search-for [break]]
]
]
print dt [Prin "IDX PTH hash-blk + block of key-val: " loop top2 [
ln: length? test
idx: 1
idx2: 2
until [idx < ln] [
k: hbobtest/:idx/:idx2
idx: idx + 1
if k = :search-for [break]
]
]
]
;--------------------------------------------------------
print dt [Prin "SLCT hash-blk + hash-blk of key-val: " loop top2 [
hhbobtest: head hhbobtest
forall hhbobtest [if select/skip hhbobtest/1 search-for 2 [break]]
]
]
print dt [Prin "PATH hash-blk + hash-blk of key-val: " loop top2 [
hhbobtest: head hhbobtest
forall hhbobtest [if hhbobtest/1/:search-for [break]]
]
]
print dt [Prin "PT hash-blk + hash-blk of key-value: " loop top2 [
ln: length? test
idx: 1
idx2: 2
until [idx < ln] [
k: hhbobtest/:idx/:idx2
idx: idx + 1
if k = :search-for [break]
]
]
]
;--------------------------------------------------------
;=== Needed when the key is a string
search-for: to-word search-for
print dt [Prin " SELECT block + object: " loop top2 [
obobtest: head obobtest
forall obobtest [if select obobtest/1 search-for [break]]
]
]
print dt [Prin " PATH block + object: " loop top2 [
obobtest: head obobtest
forall obobtest [if obobtest/1/:search-for [break]]
]
]
;------------------------------------------------------
print dt [Prin " SELECT hashed block + object: " loop top2 [
hobobtest: head hobobtest
forall hobobtest [if select hobobtest/1 search-for [break]]
]
]
print dt [Prin " PATH hashed block + object: " loop top2 [
hobobtest: head hobobtest
forall hobobtest [if hobobtest/1/:search-for [break]]
]
]
;------------------------------------------------------
;=== Results
; Block of key-value: 0:00:16.9683
; PATH Block of key-value: 0:00:04.78321
; FOREACH Block of key-value: 0:05:10.2615
; PATH IDX Block of key-value: 0:00:00.0010154
; hashed-block of key-value: 0:00:00.001013
; PATH hashed-block of key-value: 0:00:04.77653
; FOREACH hashed-Block of key-value: 0:05:11.471
; PATH IDX hashed-Block of key-value: 0:00:00.0009994
; map of key-value: 0:00:00.001001
; PATH map of key-value: 0:00:00
; block + block of key-value: 0:00:00.4314
; PATH block + block of key-value: 0:00:00.0010175
;PATH IDX block + Block of key-value: 0:00:00
; hash-blk + block of key-val: 0:11:41.6685
; PATH hash-blk + block of key-val: 0:07:50.7818
;IDX PTH hash-blk + block of key-val: 0:00:00.001517
; hash-blk + hash-blk of key-val: 0:00:01.17468
;PATH hash-blk + hash-blk of key-val: 0:00:00.0021663
;PT hash-blk + hash-blk of key-value: 0:00:00.0017118
; block + object: 0:00:00.94198
; PATH block + object: 0:00:00.0019157
; hashed block + object: 0:00:00.926419
; PATH hashed block + object: 0:00:00.0014689
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment