[REBOL] Re: Fast way to strip characters
From: pwawood::gmail at: 7-Oct-2009 21:49
Here's a slightly extended test including Pekr's stripping the
unwanted characters in place. The results show for this test:
1. Trim/with is almost 10 times faster than parse.
2. Parse is roughly 10 percent faster than parse/all.
3. Skip is slower than using the complement technique.
Here are the results for 500 speedchecks:
Average for 10000 trim/with was
0:00:00.007487802
Average for 10000 parse creating new string was
0:00:00.064615664
Average for 10000 parse/all in-situ with skip was
0:00:00.064453348
Average for 10000 parse/all in-situ with complement was
0:00:00.064148258
Average for 10000 parse in-situ with skip was
0:00:00.05863122
Average for 10000 parse in-situ with complement was
0:00:00.049951718
The test script follows.
Regards
Peter
REBOL[]
NUMBER_Of_RUNS: 500
speedtest: func [][
st: now/precise
loop 10000 [
teststring: copy "I'm so not here"
stripped: trim/with teststring "' "
]
et: now/precise
a: difference et st
st: now/precise
strip: charset [#" " #"'"]
not-strip: complement strip
loop 10000 [
teststring: copy "I'm so not here"
stripped: copy ""
parse teststring [any [copy str some not-strip (insert tail
stripped str)| some strip]]
]
et: now/precise
b: difference et st
st: now/precise
strip: charset [#" " #"'"]
loop 10000 [
teststring: copy "I'm so not here"
parse/all teststring [any [here: strip (remove here) | skip]]
]
et: now/precise
c: difference et st
st: now/precise
strip: charset [#" " #"'"]
not-strip: complement strip
loop 10000 [
teststring: copy "I'm so not here"
parse/all teststring [any [some not-strip | here: strip (remove
here)]]
]
et: now/precise
d: difference et st
st: now/precise
strip: charset [#" " #"'"]
loop 10000 [
teststring: copy "I'm so not here"
parse teststring [any [here: strip (remove here) | skip]]
]
et: now/precise
e: difference et st
st: now/precise
strip: charset [#" " #"'"]
not-strip: complement strip
loop 10000 [
teststring: copy "I'm so not here"
parse teststring [any [some not-strip | here: strip (remove here)]]
]
et: now/precise
f: difference et st
]
aa: 0
bb: 0
cc: 0
dd: 0
ee: 0
ff: 0
loop NUMBER_Of_RUNS [
speedtest
aa: aa + a
bb: bb + b
cc: cc + c
dd: dd + d
ee: ee + e
ff: ff + f
recycle
]
print ["Average for 10000 trim/with was "
aa / NUMBER_Of_RUNS]
print ["Average for 10000 parse creating new string was "
bb / NUMBER_Of_RUNS]
print ["Average for 10000 parse/all in-situ with skip was "
cc / NUMBER_Of_RUNS]
print ["Average for 10000 parse/all in-situ with complement was"
dd / NUMBER_Of_RUNS]
print ["Average for 10000 parse in-situ with skip was "
ee / NUMBER_Of_RUNS]
print ["Average for 10000 parse in-situ with complement was "
ff / NUMBER_Of_RUNS]