Speed of VB.net Regex? -
is regex code in vb.net known slow?
i took on code cleaning large amounts of text data. code ran slow, looking ways speed up. found couple functions got run lot thought might part of problem.
here's original code cleaning phone number:
dim strarray() char = strphonenum.tochararray dim strnewphone string = "" dim integer = 0 strarray.length - 1 if strarray.length = 11 , strarray(0) = "1" , = 0 continue end if if isnumeric(strarray(i)) strnewphone = strnewphone & strarray(i) end if next if len(strnewphone) = 7 or len(strnewphone) = 10 return strnewphone end if
i rewrote code eliminate array , looping using regex.
dim strnewphone string = "" strnewphone = regex.replace(strphonenum, "\d", "") if strnewphone = "" orelse strnewphone.substring(0, 1) <> "1" return strnewphone else strnewphone = mid(strnewphone, 2) end if if len(strnewphone) = 7 or len(strnewphone) = 10 return strnewphone end if
after running couple tests, new code slower old. regex in vb.net slow, did add other thing issue, or original code fine way was?
i conducted tests visual studio profiler , did not same results did. there logical error regex function caused length check missed if number didn't begin 1
. corrected in tests.
- i realized in tests, whatever function went first , last suffer penalty. executed each function independently , had priming function run before.
- depending on tests executed function either 10000 or 100000 times phone number pattern of varying length. each method got same numbers.
results
in general method faster.
- i did cheap timer test, original function twice slow.
- profiler showed original method used 60% more memory our methods.
- profiler showed original method took 8 times long work.
- profiler showed original method took 40% more processor cycles.
my conclusion
in tests original method slower. had come out better in 1 test able explain our discrepancy. ff tested methods in total isolation think come similar.
my best guess else effecting results , assessment original method better false.
your revised function
function getphonenumberregex(strphonenum string) dim strnewphone string = "" strnewphone = regex.replace(strphonenum, "\d", "") if strnewphone <> "" , strnewphone.substring(0, 1) = "1" strnewphone = mid(strnewphone, 2) end if if len(strnewphone) = 7 or len(strnewphone) = 10 return strnewphone end if return "" end function
my function
function getphonenumbermine(strphonenum string) dim strnewphone string = regex.replace(strphonenum, "\d", "") if (strnewphone.length >= 7 , strnewphone(0) = "1") strnewphone = strnewphone.remove(0, 1) end if return if(strnewphone.length = 7 orelse strnewphone.length = 10, strnewphone, "") end function
Comments
Post a Comment