我只想知道哪一个将在php中strpos()/ stripos()或preg_match()函数快。这是快速的过程strpos()/ stripos()或preg_match()在php
3
A
回答
3
我发现this blog已遇到关于你的问题有些睾丸,结果是:
- strpos()比的preg_match()
- stripos函数()快3-16倍2-30倍比strpos()
- stripos函数()比的preg_match()与 无壳改性剂快20-100%的慢 “// I”
- 使用的preg_match()正则表达式是不大于使用 长更快字符串
- 使用UTF8修饰语 “// U” 中的preg_match()使得2倍慢
的代码所用的是:
<?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();
0
标杆管理是一项棘手的业务,但可以说preg_match
比strpos
或stripos
要慢。这是因为PRCE功能实现的REGEX引擎是,其中比字符串函数更强大和更灵活。
他们也做不同的事情。 strpos
会告诉你另一个字符串内字符串的开始索引,而preg_match
主要用于探测字符串的格式,并根据正则表达式检索字符串的部分。
总之,如果您只是想在另一个字符串中查找字符串,请使用strpos
或stripos
。
1
preg_match
是三者中最慢的。 stripos
将比strpos
慢,因为它将不得不做额外的工作来处理不区分大小写的匹配。
相关问题
- 1. preg_match()或stripos()?
- 2. strpos或pregmatch所有在PHP vs MariaDB LIKE或IN查询,这是快?
- 3. 这是快速URLConnection或套接字
- 4. PHP stristr与stripos
- 5. preg_match for。 /或\在PHP
- 6. PHP,Zend Framework快速入门教程:这是如何工作的?
- 7. 在逻辑过滤中使用PHP stripos()
- 8. 我在做什么错? strpos,strlen和preg_match
- 9. 如何使用strpos()insted的的preg_match()的
- 10. PHP:这是什么模式? (的preg_match)
- 11. 什么是strpos和or stripos的正确使用方法? (这是一个错误吗?)
- 12. strpos/stripos函数(严格的比较)未能
- 13. MySQL的插入速度比PHP更快,这是预期的吗?
- 14. PHP - Strpos在阵列
- 15. 如何SUBSTR或strpos这个角色
- 16. PHP使用Stripos和array_push
- 17. 这是什么呢,的preg_match
- 18. 的preg_match():在PHP
- 19. 匹配通过PHP的preg_match
- 20. 是通过值或引用快速传递的条件绑定?
- 21. 在PHP中Strpos的问题
- 22. 通过ZipArchive快速解压比在php中使用exec()更快?
- 23. QSqlTableModel与QSqlQuery,这是快速(通过互联网)?
- 24. PHP会话快速随机过期
- 25. PHP - 快速通过图像像素
- 26. 快速帮助。在PHP
- 27. php preg_match或preg_replace验证
- 28. 在PHP中快速查询相关内容的快速问题!
- 29. preg_match在PHP for“&$”
- 30. PHP strpos问题