I've seen a developer coded a method today to validate a text string, return true if the text contains digit only (0-9), return false if either one of the character in the text contain non-digit, the method is ~6 lines long, find and loop through each character.
There's an easier way to achieve the same result - use of Regular expression.
The method can be simplified to 1 line.
This change, saved 0.31ms (from the trace captured), although the number looks small, but when this is executed millions time, it does cost a lot.
In the sample file I received, it imports around 20 million lines, given the 0.31ms saving, that reduce the execution time by:
> Time saved in ms x 20m lines = 6,200,000ms
> Time saved in seconds = 6,200 seconds
> Time saved in minutes = 103 minutes
> Time saved in hours = 1.72 hours
Using regular expression ===============================
//Return TRUE if '_text' contain digit only, otherwise return FALSE
boolean validateTextMatch(str _text)
{
;
return !match("[^0-9]", _text);
}
Using strFind() & subStr() in loop =========================
boolean
validateTextLoop(str _text)
{
int counter, textLen = strlen(_text);
Phone validNumbers = '0123456789';
;
for(counter = 1; counter <= textLen; counter ++)
{
if(!strfind(substr(_text,counter,1), validNumbers, 1, 10))
{
return false;
}
}
return true;
}
Screenshot showing the trace time of both methods |
For Regular expression syntax reference, visit this link: http://msdn.microsoft.com/en-us/library/aa886279.aspx
More usage of Trace Parser at these links:
http://mybhat.blogspot.co.uk/2011/11/performance-tuning-using-trace-parser.html
http://mybhat.blogspot.co.uk/2012/05/joining-permanent-table-with-temp-table.html
More usage of Trace Parser at these links:
http://mybhat.blogspot.co.uk/2011/11/performance-tuning-using-trace-parser.html
http://mybhat.blogspot.co.uk/2012/05/joining-permanent-table-with-temp-table.html
This comment has been removed by a blog administrator.
ReplyDeleteHi, thanks for sharing such a good post. In my case, the erp systems work the best for my company. Now we use dynamics 365 development and it's probably the most efficient software, and for sure the easiest one to use. If you have any questions, google anegis consulting - they will be happy to answer them all.
ReplyDelete