- strpos() is 3-16 times faster than preg_match()
- stripos() is 2-30 times slower than strpos()
- stripos() is 20-100 percent faster than preg_match() with the caseless modifier "//i"
- using a regular expression in preg_match() is not faster than using a long string
- using the utf8 modifier "//u" in preg_match() makes it 2 times slower
Here is a sample script to compare the functions with different string sizes:
<?php
function loop(){
$str_50 = str_repeat('a', 50).str_repeat('b', 50);
$str_100 = str_repeat('a', 100).str_repeat('b', 100);
$str_500 = str_repeat('a', 250).str_repeat('b', 250);
$str_1k = str_repeat('a', 1024).str_repeat('b', 1024);
$str_10k = str_repeat('a', 10240).str_repeat('b', 1024);
$str_100k = str_repeat('a', 102400).str_repeat('b', 1024);
$str_500k = str_repeat('a', 1024*500).str_repeat('b', 1024);
$str_1m = str_repeat('a', 1024*1024).str_repeat('b', 1024);
$b = 'b';
$b_10 = str_repeat('b', 10);
$b_100 = str_repeat('b', 100);
$b_1k = str_repeat('b', 1024);
echo str_replace(',', "\t", ',strpos,preg,preg U,preg S,preg regex,stripos,preg u,'.
'preg i,preg u i,preg i regex,stripos uc,preg i uc,preg i uc regex').PHP_EOL;
foreach (array($b, $b_10, $b_100, $b_1k) as $needle) {
foreach (array($str_50, $str_100, $str_500, $str_1k, $str_10k,
$str_100k, $str_500k, $str_1m) as $str) {
echo strlen($needle).'/'.strlen($str);
$start = mt();
for ($i=0; $i<25000; $i++) $j = strpos($str, $needle); // strpos
echo "\t".mt($start);
$regex = '!'.$needle.'!';
$start = mt();
for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg
echo "\t".mt($start);
$regex = '!'.$needle.'!U';
$start = mt();
for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg Ungreedy
echo "\t".mt($start);
$regex = '!'.$needle.'!S';
$start = mt();
for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg extra analysiS
echo "\t".mt($start);
$regex = "!b{".strlen($needle)."}!";
$start = mt();
for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg regex
echo "\t".mt($start);
$start = mt();
for ($i=0; $i<25000; $i++) $j = stripos($str, $needle); // stripos
echo "\t".mt($start);
$regex = '!'.$needle.'!u';
$start = mt();
for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg utf-8
echo "\t".mt($start);
$regex = '!'.$needle.'!i';
$start = mt();
for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg i
echo "\t".mt($start);
$regex = '!'.$needle.'!ui';
$start = mt();
for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg i utf-8
echo "\t".mt($start);
$regex = "!b{".strlen($needle)."}!i";
$start = mt();
for ($i=0; $i<25000; $i++) $j = preg_match($regex, $str); // preg i regex
echo "\t".mt($start);
echo PHP_EOL;
}
echo PHP_EOL;
}
}
function mt($start=null){
if ($start === null) return microtime(true);
return number_format(microtime(true)-$start, 4);
}
loop();
Running with PHP 5.4.4, 64bit, 2.3GHz (QEMU):
strpos | preg | preg U | preg S | preg regex | stripos | preg u | preg i | preg u i | preg i regex | ||||
1/100 | 0.0052 | 0.0144 | 0.0147 | 0.0172 | 0.0168 | 0.0102 | 0.0180 | 0.0153 | 0.0195 | 0.0143 | |||
1/200 | 0.0058 | 0.0177 | 0.0166 | 0.0159 | 0.0143 | 0.0121 | 0.0229 | 0.0160 | 0.0236 | 0.0159 | |||
1/500 | 0.0061 | 0.0237 | 0.0246 | 0.0236 | 0.0219 | 0.0215 | 0.0394 | 0.0217 | 0.0383 | 0.0212 | |||
1/2048 | 0.0087 | 0.0589 | 0.0590 | 0.0580 | 0.0359 | 0.0645 | 0.1205 | 0.0499 | 0.1077 | 0.0472 | |||
1/11264 | 0.0221 | 0.2693 | 0.2672 | 0.2640 | 0.2443 | 0.3115 | 0.5939 | 0.3631 | 0.6803 | 0.3662 | |||
1/103424 | 0.1867 | 2.3432 | 2.3384 | 2.3575 | 2.3211 | 2.9327 | 5.3040 | 3.4689 | 6.4391 | 3.4750 | |||
1/513024 | 1.0228 | 11.4006 | 11.3530 | 12.0689 | 11.3858 | 14.5023 | 26.0701 | 17.2658 | 31.6543 | 16.9941 | |||
1/1049600 | 2.0988 | 23.3561 | 23.3382 | 23.3811 | 23.4459 | 30.2119 | 52.9937 | 34.9654 | 64.4141 | 35.1033 | |||
10/100 | 0.0055 | 0.0167 | 0.0171 | 0.0166 | 0.0148 | 0.0104 | 0.0199 | 0.0158 | 0.0215 | 0.0150 | |||
10/200 | 0.0053 | 0.0165 | 0.0155 | 0.0180 | 0.0151 | 0.0125 | 0.0222 | 0.0167 | 0.0264 | 0.0175 | |||
10/500 | 0.0065 | 0.0247 | 0.0226 | 0.0219 | 0.0179 | 0.0207 | 0.0391 | 0.0222 | 0.0376 | 0.0236 | |||
10/2048 | 0.0088 | 0.0590 | 0.0578 | 0.0625 | 0.0371 | 0.0649 | 0.1192 | 0.0503 | 0.1108 | 0.0484 | |||
10/11264 | 0.0212 | 0.2701 | 0.2652 | 0.2617 | 0.2412 | 0.3068 | 0.5838 | 0.3553 | 0.6652 | 0.3491 | |||
10/103424 | 0.1542 | 2.3274 | 2.2972 | 2.2938 | 2.2765 | 2.7586 | 5.1789 | 3.4096 | 6.3119 | 3.3939 | |||
10/513024 | 0.7236 | 11.4388 | 11.4880 | 11.4932 | 11.3312 | 14.1851 | 25.7927 | 17.1388 | 32.1902 | 17.5287 | |||
10/1049600 | 1.4951 | 23.4072 | 23.2000 | 23.4175 | 23.2439 | 29.6794 | 53.0526 | 34.9641 | 64.9861 | 35.9626 | |||
100/100 | 0.0063 | 0.1963 | 0.1956 | 0.0186 | 0.1264 | 0.0146 | 0.2922 | 0.2061 | 0.2281 | 0.1397 | |||
100/200 | 0.0060 | 0.0234 | 0.0219 | 0.0219 | 0.0212 | 0.0180 | 0.0296 | 0.0309 | 0.0422 | 0.0235 | |||
100/500 | 0.0071 | 0.0304 | 0.0297 | 0.0291 | 0.0250 | 0.0273 | 0.0452 | 0.0376 | 0.0559 | 0.0310 | |||
100/2048 | 0.0103 | 0.0671 | 0.0681 | 0.0676 | 0.0428 | 0.0762 | 0.1329 | 0.0665 | 0.1239 | 0.0516 | |||
100/11264 | 0.0210 | 0.2625 | 0.2623 | 0.2642 | 0.2422 | 0.3067 | 0.5762 | 0.3600 | 0.6774 | 0.3548 | |||
100/103424 | 0.1474 | 2.2864 | 2.2856 | 2.3114 | 2.2968 | 2.7858 | 5.2261 | 3.4506 | 6.3282 | 3.4003 | |||
100/513024 | 0.7241 | 11.4054 | 11.4286 | 11.3435 | 11.5313 | 14.1507 | 25.6885 | 17.1702 | 31.8195 | 17.1162 | |||
100/1049600 | 1.4736 | 23.2258 | 23.4724 | 23.2595 | 23.3052 | 29.4276 | 53.4051 | 35.2015 | 64.3818 | 34.6691 | |||
1024/100 | 0.0047 | 0.2033 | 0.2041 | 0.0517 | 0.1042 | 0.0083 | 0.2899 | 0.2092 | 0.2290 | 0.1204 | |||
1024/200 | 0.0049 | 0.0562 | 0.0554 | 0.0563 | 0.2900 | 0.0109 | 0.0615 | 0.5340 | 0.6496 | 0.3281 | |||
1024/500 | 0.0049 | 0.0647 | 0.0638 | 0.0637 | 1.3358 | 0.0181 | 0.0761 | 2.5063 | 3.2811 | 1.4076 | |||
1024/2048 | 0.0069 | 0.0955 | 0.0954 | 0.0963 | 0.0688 | 0.0887 | 0.1526 | 0.1536 | 0.2367 | 0.0809 | |||
1024/11264 | 0.0195 | 0.2974 | 0.2982 | 0.3000 | 0.2817 | 0.3375 | 0.6168 | 0.4718 | 0.8103 | 0.3845 | |||
1024/103424 | 0.1524 | 2.3356 | 2.3316 | 2.3477 | 2.3338 | 2.8351 | 5.2727 | 3.5535 | 6.4703 | 3.4583 | |||
1024/513024 | 0.7217 | 11.4283 | 11.5377 | 11.4987 | 11.4162 | 14.2862 | 25.8131 | 17.1762 | 33.1633 | 18.1542 | |||
1024/1049600 | 1.4657 | 23.3272 | 23.2205 | 23.2752 | 23.6812 | 29.1804 | 54.9962 | 34.9720 | 64.7529 | 37.1609 | |||
Running with HHVM 3.0/2014.03.27, 64bit, 2.3GHz (QEMU):
strpos | preg | preg U | preg S | preg regex | stripos | preg u | preg i | preg u i | preg i regex | ||||
1/100 | 0.0028 | 0.0081 | 0.0085 | 0.0079 | 0.0079 | 0.0039 | 0.0124 | 0.0084 | 0.0125 | 0.0085 | |||
1/200 | 0.0021 | 0.0093 | 0.0094 | 0.0092 | 0.0094 | 0.0045 | 0.0167 | 0.0091 | 0.0162 | 0.0088 | |||
1/500 | 0.0021 | 0.0160 | 0.0183 | 0.0168 | 0.0121 | 0.0102 | 0.0341 | 0.0154 | 0.0309 | 0.0138 | |||
1/2048 | 0.0051 | 0.0541 | 0.0531 | 0.0523 | 0.0302 | 0.0374 | 0.1118 | 0.0396 | 0.0980 | 0.0394 | |||
1/11264 | 0.0176 | 0.2531 | 0.2537 | 0.2541 | 0.2373 | 0.3415 | 0.5682 | 0.3451 | 0.6602 | 0.3436 | |||
1/103424 | 0.1781 | 2.2955 | 2.2846 | 2.2814 | 2.2535 | 3.3880 | 5.2139 | 3.3744 | 6.2748 | 3.4123 | |||
1/513024 | 1.0425 | 11.3477 | 11.6653 | 11.4149 | 11.3018 | 16.9227 | 26.7142 | 17.2595 | 31.6541 | 17.2308 | |||
1/1049600 | 2.1241 | 23.2635 | 23.2525 | 23.3054 | 23.2256 | 34.9508 | 52.7012 | 34.9024 | 64.5221 | 34.9430 | |||
10/100 | 0.0013 | 0.0084 | 0.0084 | 0.0086 | 0.0076 | 0.0040 | 0.0126 | 0.0086 | 0.0119 | 0.0077 | |||
10/200 | 0.0014 | 0.0089 | 0.0091 | 0.0091 | 0.0088 | 0.0063 | 0.0158 | 0.0108 | 0.0167 | 0.0095 | |||
10/500 | 0.0020 | 0.0155 | 0.0158 | 0.0157 | 0.0120 | 0.0129 | 0.0308 | 0.0156 | 0.0308 | 0.0147 | |||
10/2048 | 0.0073 | 0.0500 | 0.0496 | 0.0505 | 0.0295 | 0.0477 | 0.1088 | 0.0410 | 0.1032 | 0.0397 | |||
10/11264 | 0.0169 | 0.2536 | 0.2522 | 0.2519 | 0.2312 | 0.4572 | 0.5668 | 0.3498 | 0.6614 | 0.3443 | |||
10/103424 | 0.1829 | 2.3130 | 2.2926 | 2.3003 | 2.2826 | 4.5526 | 5.2329 | 3.4511 | 6.4243 | 3.4623 | |||
10/513024 | 1.0610 | 11.5358 | 11.5248 | 11.4948 | 11.4198 | 22.9372 | 25.9165 | 17.0674 | 31.4214 | 16.9746 | |||
10/1049600 | 2.0783 | 23.8131 | 23.7319 | 23.1988 | 23.2098 | 46.5398 | 53.5480 | 35.4728 | 65.4561 | 35.6284 | |||
100/100 | 0.0012 | 0.0065 | 0.0062 | 0.0066 | 0.0058 | 0.0017 | 0.0112 | 0.0066 | 0.0101 | 0.0059 | |||
100/200 | 0.0013 | 0.0102 | 0.0101 | 0.0103 | 0.0118 | 0.0106 | 0.0172 | 0.0173 | 0.0250 | 0.0126 | |||
100/500 | 0.0018 | 0.0165 | 0.0163 | 0.0166 | 0.0149 | 0.0170 | 0.0320 | 0.0219 | 0.0383 | 0.0176 | |||
100/2048 | 0.0042 | 0.0505 | 0.0503 | 0.0508 | 0.0318 | 0.0510 | 0.1090 | 0.0473 | 0.1085 | 0.0432 | |||
100/11264 | 0.0178 | 0.2556 | 0.2535 | 0.2552 | 0.2356 | 0.4605 | 0.5690 | 0.3537 | 0.6825 | 0.3559 | |||
100/103424 | 0.1764 | 2.2992 | 2.3004 | 2.2966 | 2.2881 | 4.5522 | 5.1924 | 3.4346 | 6.2967 | 3.4354 | |||
100/513024 | 1.0296 | 11.3657 | 11.3776 | 11.3046 | 11.4125 | 22.5889 | 26.0071 | 16.9886 | 32.3710 | 16.9588 | |||
100/1049600 | 2.1064 | 23.4239 | 23.3236 | 23.2174 | 23.1929 | 46.4756 | 53.4129 | 34.8542 | 65.2457 | 35.4284 | |||
1024/100 | 0.0013 | 0.0116 | 0.0117 | 0.0121 | 0.0060 | 0.0009 | 0.0216 | 0.0122 | 0.0154 | 0.0058 | |||
1024/200 | 0.0009 | 0.0151 | 0.0151 | 0.0154 | 0.0071 | 0.0009 | 0.0279 | 0.0139 | 0.0201 | 0.0074 | |||
1024/500 | 0.0009 | 0.0216 | 0.0216 | 0.0219 | 0.0104 | 0.0009 | 0.0426 | 0.0190 | 0.0331 | 0.0132 | |||
1024/2048 | 0.0030 | 0.0557 | 0.0556 | 0.0562 | 0.0632 | 0.0939 | 0.1197 | 0.1173 | 0.1947 | 0.0742 | |||
1024/11264 | 0.0161 | 0.2624 | 0.2590 | 0.2588 | 0.2659 | 0.4981 | 0.5789 | 0.4164 | 0.7576 | 0.3794 | |||
1024/103424 | 0.1467 | 2.2902 | 2.3172 | 2.2994 | 2.2857 | 4.5648 | 5.2119 | 3.4726 | 6.3626 | 3.4485 | |||
1024/513024 | 0.7160 | 11.3871 | 11.4345 | 11.3034 | 11.4561 | 22.7561 | 25.8564 | 17.0333 | 31.4014 | 16.9700 | |||
1024/1049600 | 1.4864 | 23.1771 | 23.1033 | 23.4143 | 23.3364 | 47.5513 | 55.7561 | 36.7900 | 64.7846 | 35.1457 | |||
0 comments:
Post a Comment