zoukankan      html  css  js  c++  java
  • php 判断字符串之间包含关系

    之前常用stristr ,  strpos判断。

    因为处理1000W * 1000W级别,循环就是漫长漫长...

    在此,对stristr, strpos, explode判断字符串包含关系处理速度对比:

    Header("Content-Type:text/html;charset=utf-8");
    ini_set('memory_limit','-1');
    set_time_limit(0);
    
    
    
    $count = 10000000;//循环次数
    
    $str1 = 'abcdef';
    $str2 = 'abce';
    $str3 = 'abc';
    
    function str_is_include($str1, $str2){//strpos判断包含关系
    
        if( strpos($str1, $str2) !== false )    return true;
        else return false;
    }
    
    function str_is_include2($str1, $str2){//explode判断包含关系
        
        $arr = explode($str2, $str1);
        if(count($arr) > 0)    return true;
        else return false;
    }
    
    $q = time();
    for($i=0; $i<$count; $i++){
        
        if( strpos($str1, $str2)!== false ){}
        if( strpos($str1, $str3) !== false ){}//此处小插曲; 本来str_is_include($str1,$str2)来判断。 但是输出strpos耗时:17秒 跟stristr同一个级别。 于是考虑循环内部不去取外部函数。省掉单独为函数开辟堆栈时间。1000W次就生下来了10秒。
    } echo '循环strpos耗时:'.(time()-$q).'<br/>'; 
    $w = time();
    for($i=0; $i<$count; $i++){
      
    stristr($str1, $str2);
      
    stristr($str1, $str3);
    }
    echo '循环stristr耗时:'.(time()-$w).'<br/>';
    $e = time();
    for($i=0; $i<$count; $i++){
      str_is_include2(
    $str1, $str2);
      str_is_include2(
    $str1, $str3);
    }
    echo '循环explode耗时:'.(time()-$e);

    //////////////////////////////////////////////////////(秒)
    循环strpos耗时:7
    循环stristr耗时:17
    循环explode耗时:29

     strpos 胜出!!!

  • 相关阅读:
    一月十三号学习日报
    一月十四号学习日报
    一月六号学习日报
    ARP欺骗
    一月十一号学习日报
    vscode文件名重叠
    vue : 无法加载文件 C:Users1111111AppDataRoaming pmvue.ps1,因为在此系统禁止运行脚本
    成绩录入和查询
    node搭建服务器
    class和id的区别
  • 原文地址:https://www.cnblogs.com/qunshu/p/3221037.html
Copyright © 2011-2022 走看看