Home Backend Development PHP Tutorial PHP字符串单字气节作――substr真的好慢

PHP字符串单字气节作――substr真的好慢

Jun 13, 2016 pm 01:03 PM
br float gt lt

PHP字符串单字节操作――substr真的好慢

当我们要操作字符串中的某一个单字节时,我们该使用什么样的方式操作?一直以为,substr函数不会很慢。但今天测试了一下,结果并非如此。以下是测试代码:

?

<?php
    $str='abcdefghijklmnopqrstuvwxyz1234567890';
    error_reporting(E_ALL);
    /**
     * Simple function to replicate PHP 5 behaviour
     */
    function microtime_float()
    {
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }
    //使用substr函数
    $start=microtime_float();
    for ($k = 100; $k > 0; $k--) { 
        for ($i=0;$i<strlen($str);$i++)
           echo substr($str,$i,1), ' ';
    }
    $end = microtime_float();
    echo("<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/> t i m e :" .  round( $end - $start ,6) ."<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>");

    

   //使用字节操作
    $start=microtime_float();
    for ($k = 100; $k > 0; $k--) {   
        for ($i=0;$i<strlen($str);$i++)
           echo $str{$i}, ' ';
    }
   $end = microtime_float();

    echo("<br/> <br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>t i m e :" .  round( $end - $start ,6) ."<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>");
    
    
    //使用数组模式操作
    $start=microtime_float();
    for ($k = 100; $k > 0; $k--) {    
        for ($i=0;$i<strlen($str);$i++)
           echo $str[$i], ' ';
    }
    $end = microtime_float();

    echo("<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/> t i m e :" .  round( $end - $start ,6) ."<br/><br/><br/><br/><br/><br/><br/><br/><br/><br/><br/>");
    
    

?>
Copy after login

?以下是测试结果:

t i m e :0.003782
t i m e :0.002862
t i m e :0.002587

t i m e :0.00351
t i m e :0.002649
t i m e :0.002786

由此可见,substr速度是很慢的。而字节操作与数组操作模式,则是同一级别的。由此我们认为,如果对字符串进行字节操作,则使用字节操作模式最好,因为,这一方式不会导致误以为变量类型是数组。
当然,这种操作方式有其局限性,那就是对于中文字串是无法这样处理的,你只有使用mb_substr!!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

What are the differences between Huawei GT3 Pro and GT4?

What is the maximum value of float? What is the maximum value of float? Oct 11, 2023 pm 05:54 PM

What is the maximum value of float?

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Fix: Snipping tool not working in Windows 11

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

How to Fix Can't Connect to App Store Error on iPhone

What are the database float lengths? What are the database float lengths? Oct 10, 2023 pm 03:57 PM

What are the database float lengths?

What does float mean in c language? What does float mean in c language? Oct 12, 2023 pm 02:30 PM

What does float mean in c language?

What is the accuracy of float? What is the accuracy of float? Oct 17, 2023 pm 03:13 PM

What is the accuracy of float?

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

See all articles