PHP speed question

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Hypothetically,

Which one is faster in this specific example? I love regular expressions to bits, but just curious whether they should be used ALL the time.

PHP:
<php
		$value = "nobody@nowhere.co.za";
		// preg_match is most flexible I know
		if (!preg_match("/@/", $value)) {
			echo "Validation failed.<br />";
		}
		// strpos vs preg_match from a performance perspective?
		if (strpos($value, "@") === false) {
		  echo "Validation failed.<br />";
		}
		
?>
 

^^vampire^^

Expert Member
Joined
Feb 17, 2009
Messages
3,877
Languages have built in functions for the reason that they are usually well benchmarked and tested to make sure that they are the fastest way to it. Not always the case but usually is as they will be built at a lower level than your code.
 

gkm

Expert Member
Joined
May 10, 2005
Messages
1,519
It one thinks about how much work the CPU must do for something complex like processing a regex, vs something trivial like finding a character in a string, the situation is no contest. Regex's are very useful, but should in general be considered expensive, so use only when they avoid some other complex code.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
It one thinks about how much work the CPU must do for something complex like processing a regex, vs something trivial like finding a character in a string, the situation is no contest. Regex's are very useful, but should in general be considered expensive, so use only when they avoid some other complex code.

Yea that is what I was thinking use regex only when truly needed as there is some pay off for its powerful ability.
 

halfmoonforever

Expert Member
Joined
Feb 1, 2016
Messages
1,196
If the only condition for your "valid email" check is to check for an @ sign, then strpos, if it's much more than that, then filter, if that doesn't validate a special TLD or something of the like you would need to specifically cater for, then regular expression
 

Other Pineapple Smurf

Honorary Master
Joined
Jun 21, 2008
Messages
14,593
Here is your answer: http://we-love-php.blogspot.co.za/2014/03/strpos-vs-pregmatch-vs-stripos.html

strpos is faster.

Only use preg_match or any other regex matching when you cannot use strpos as you need to keep your code easy to read. While more experienced developers will easily read regex it becomes harder for more junior and even intermediate devs.

My work email signature: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.
 

Other Pineapple Smurf

Honorary Master
Joined
Jun 21, 2008
Messages
14,593
If the only condition for your "valid email" check is to check for an @ sign, then strpos, if it's much more than that, then filter, ...

+1

...if that doesn't validate a special TLD or something of the like you would need to specifically cater for, then regular expression

Dear Lord, please don't ever use regex to validate domain. Have you seen the monster it is and you need to constantly update it. Then it only validates that it is a possible valid email domain.

2 years ago I had the problem with the existing regex being out of date so I tried MX lookups of the domain to rather validate it. Never went into production as we where concerned about performance but it was a proof of concept and worked well. If you ever really want to validate then there is only a few common domains (gmail, discovery, etc) that you could hard code into a list and then the rest you could do MX lookups.
 

DA-LION-619

Honorary Master
Joined
Aug 22, 2009
Messages
13,777
Here is your answer: http://we-love-php.blogspot.co.za/2014/03/strpos-vs-pregmatch-vs-stripos.html

strpos is faster.

Only use preg_match or any other regex matching when you cannot use strpos as you need to keep your code easy to read. While more experienced developers will easily read regex it becomes harder for more junior and even intermediate devs.

My work email signature: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

:D
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Here is your answer: http://we-love-php.blogspot.co.za/2014/03/strpos-vs-pregmatch-vs-stripos.html

strpos is faster.

Only use preg_match or any other regex matching when you cannot use strpos as you need to keep your code easy to read. While more experienced developers will easily read regex it becomes harder for more junior and even intermediate devs.

My work email signature: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

Shiat those stats!!

Holy hell. I really liked regular expressions since it felt natural and immensely powerfull.


Now I will try to never ever use it in my code.
 

Other Pineapple Smurf

Honorary Master
Joined
Jun 21, 2008
Messages
14,593
Here is the email validation regex in PHP:

PHP:
/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/iD

source: http://emailregex.com/
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
Here is the email validation regex in PHP:

PHP:
/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/iD

source: http://emailregex.com/

I know this.


Read the post where I said I AM NOT using that as validation or anything I DO NOT use that code it was merely for illustrative purposes
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
Shiat those stats!!

Holy hell. I really liked regular expressions since it felt natural and immensely powerfull.


Now I will try to never ever use it in my code.

Once off usage is rarely ever going to an issue e.g. email validation.

You should only be looking for faster alternatives when you're, for example:
  • processing a large dataset in a loop.
 

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
[)roi(];18179625 said:
Once off usage is rarely ever going to an issue e.g. email validation.

You should only be looking for faster alternatives when you're, for example:
  • processing a large dataset in a loop.

That's what I currently used it for hehehe.


In the loop I am removing some pipes and funny character from a string begotten from the api loop runs for 300 currency items lel.

Clearly not efficient so I'll be "optimizing tonight"
 

biometrics

Honorary Master
Joined
Aug 7, 2003
Messages
71,858
That's what I currently used it for hehehe.


In the loop I am removing some pipes and funny character from a string begotten from the api loop runs for 300 currency items lel.

Clearly not efficient so I'll be "optimizing tonight"

300 is not large. You're wasting your time.
 

[)roi(]

Executive Member
Joined
Apr 15, 2005
Messages
6,282
That's what I currently used it for hehehe.


In the loop I am removing some pipes and funny character from a string begotten from the api loop runs for 300 currency items lel.

Clearly not efficient so I'll be "optimizing tonight"


As @biometrics said; don't bother, that's definetly not large...

When in doubt remember this:
"Programmers waste enormous amounts of time thinking about, or worrying about, the speed of noncritical parts of their programs, and these attempts at efficiency actually have a strong negative impact when debugging and maintenance are considered. We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%." -- Donald Knuth

http://c2.com/cgi/wiki?PrematureOptimization
 
Last edited:

Thor

Honorary Master
Joined
Jun 5, 2014
Messages
44,236
300 is not large. You're wasting your time.

[)roi(];18179673 said:
As @biometrics said; don't bother, that's definetly not large...

When in doubt remember this:

Each loop irreteration takes 384ms

So 300 x 384ms

Then the regex takes 0.025ms

So it's 300 x (384 + 0.025)


I can't do anything on the API speed but I can use something else instead of regular expression.
 
Top