这是PHP中的快速进程strpos()/stripos()或preg_match() [英] which is the fast process strpos()/stripos() or preg_match() in php

查看:230
本文介绍了这是PHP中的快速进程strpos()/stripos()或preg_match()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想知道php中的strpos()/stripos()或preg_match()函数最快的一个.

I just want to known which one will be fast of strpos()/stripos() or preg_match() functions in php.

推荐答案

我发现

I found this blog that has run some testes regarding your question, the result was:

  • strpos()比preg_match()快3-16倍
  • stripos()比strpos()慢2-30倍
  • stripos()比preg_match()快20-100%, 不区分大小写的修饰符"//i"
  • 在preg_match()中使用正则表达式并不比使用a 长字符串
  • 在preg_match()中使用utf8修饰符"//u"会使速度慢2倍
  • 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

使用的代码是:

<?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();

这篇关于这是PHP中的快速进程strpos()/stripos()或preg_match()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆