Created
January 11, 2012 20:25
-
-
Save vicb/1596588 to your computer and use it in GitHub Desktop.
substr vs strpos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$str = str_repeat('*!', 10); | |
$start = microtime(true); | |
for ($i = 0; $i < 100000; $i++) { | |
substr($str, 0, 1); | |
} | |
printf("substr took: %d\n", (microtime(true) - $start) * 1000); | |
$start = microtime(true); | |
for ($i = 0; $i < 100000; $i++) { | |
strpos($str, '@'); | |
} | |
printf("strpos took: %d\n", (microtime(true) - $start) * 1000); |
+1 substr vs strpos
spoler alert: strpos is consistently faster.
On my end, after 10 checks and increased loops, it was the other way around by at least 1,5x
substr took: 85
strpos took: 129
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
spoler alert: strpos is consistently faster.