Hi all, I would like to know which one is faster. A regex or writing our own validation code?
For example I have a string which has some tabs at the start. I want to find the number of beginning tabs or spaces.
Simply writing a for loop and using the charAt() method is quick or regex is quick? And can anyone please explain why?
I have read a post here which says that iterating through the text and finding the spaces and tabs is quick instead of using regex.
If iterating using charAt is quick, then what if I want to validate an email?
1. Check whether the first character is an alphabet or not. If alphabet then
2. Simply iterate through the email to check if it has an '@' character. While iterating using charAt, I will append the chars to a StringBuffer. When I find '@' then I will append the rest characters to another StringBuffer. I want to skip split() (or) substring() methods. If '@' character is found then
4. Then iterate the first string whether it has any special characters other than _ and .(dot). If valid then
5. Check the second string does not have any special characters other than .(dot)
Suppose if this is how I am going to validate an email id. Then is this quick than a regex?
Strive for clarity first and foremost, not micro-optimizations in speed.
If a simple regex can prevent you from having to write a convoluted mess of loops and conditions, go with the regex approach. On the otherhand, if a regex becomes impossibly hard to understand, you might want to reconsider the regex approach - example:
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
The nice thing about regexes is that you may be able to search and find a regex already made. For validating internet e-mail addresses, you can use a simple little regex which will fit easily into two pages
chaitanya karthikk wrote:Hi all, I would like to know which one is faster. A regex or writing our own validation code?
Faster in terms of what? Writing the code? execution? Debugging?
Look at it this way. If you are going to run the validation a million times, the difference between the two might be as much as a second.
Writing one vs the other will more than make up that time. And, if you have to go back and debug it later, one may take hours to fix, while the other only a few minutes.
I'm not saying which is which, since it depends on what you are more familiar with. But choosing one vs. the other based on execution time is pointless. You've spent more time worrying about that than you will ever save by choosing the 'faster' one.
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Thank you all. I just wanted to know how iteration is fast than using regex. I dint say that I am going to use this approach. Definitely a single line code is easily understood than writing a bunch of lines to make it execute quickly. I was curious. So I asked this question.