strip chars
Richard Gaskin
ambassador at fourthworld.com
Fri Jul 25 10:06:21 EDT 2008
RegEx solutions are conveniently compact to write, but given the
complexity of the RegEx subsystem they tend to be slower than more
brute-force solutions.
Being a benchmarking fan I couldn't resist testing this latest case,
extracting numerals from an alphanumeric string.
Below is a test handler which calls two functions, the first using RegEx
and the second using "repeat for each". To run just put a button and
three fields on a card, and put some alphanumeric test into the first
field. It takes the text from field 1 and runs it through each of the
two functions, putting the results into flds 2 and 3 and then displaying
in the Message Box the execution times and whether the results matched
(useful to make sure the algos are doing the same job).
In my tests here on strings long and short the "repeat for each" method
seems roughly twice as fast as the RegEx one.
on mouseup
put fld 1 into tSrc
put 1000 into n
--
-- ReplaceText:
--
put the millisecs into t
repeat n
get rplText(tSrc)
end repeat
put the millisecs - t into t1
put it into fld 2
--
-- Repeat for Each:
--
put the millisecs into t
repeat n
get rptText(tSrc)
end repeat
put the millisecs - t into t2
put it into fld 3
--
-- Results:
put t1 && t2 &cr& (fld 2 = fld 3)
end mouseup
function rplText pSrc
return replacetext( pSrc ,"[^0-9]","")
end rplText
function rptText pSrc
put empty into s
repeat for each char k in pSrc
if k is in "0123456789" then put k after s
end repeat
return s
end rptText
--
Richard Gaskin
Managing Editor, revJournal
_______________________________________________________
Rev tips, tutorials and more: http://www.revJournal.com
More information about the use-livecode
mailing list