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 AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

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)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
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

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

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

Maximum value of float: 1. In C language, the maximum value of float is 3.40282347e+38. According to the IEEE 754 standard, the maximum exponent of the float type is 127, and the number of digits of the mantissa is 23. In this way, the maximum floating point number is 3.40282347 e+38; 2. In the Java language, the maximum float value is 3.4028235E+38; 3. In the Python language, the maximum float value is 1.7976931348623157e+308.

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

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

What does float32 bytes include? What does float32 bytes include? Oct 10, 2023 pm 04:07 PM

The float32 byte includes the sign bit, exponent bit and mantissa bit, and is used to represent 32-bit floating point numbers. Detailed introduction: 1. Sign bit (1 bit), used to represent the sign of a number, 0 represents a positive number, 1 represents a negative number; 2. Exponent bit (8 bits), used to represent the exponent part of a floating point number, through the exponent bit , you can adjust the size range of the floating-point number; 3. The mantissa bit (23 bits) is used to represent the mantissa part of the floating-point number, and the mantissa bit stores the decimal part of the floating-point number. The sign bit determines the sign of a floating point number, and the exponent bit and the mantissa bit jointly determine the size and precision of the floating point number.

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

The precision of float can reach 6 to 9 decimal places. According to the IEEE754 standard, the number of significant digits that the float type can represent is approximately 6 to 9 digits. It should be noted that this is only the theoretical maximum precision. In actual use, due to the rounding error of floating point numbers, the precision of the float type is often lower. When performing floating-point number operations in a computer, precision loss may occur due to the precision limitations of floating-point numbers. In order to improve the precision of floating point numbers, you can use higher precision data types, such as double or long double.

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

Float in C language is a data type used to represent single-precision floating point numbers. Floating point numbers are real numbers represented in scientific notation and can represent very large or very small values. Variables of the float type can store values ​​with 6 significant digits after the decimal point. In C language, the float type can be used to operate and store floating point numbers. Its variables can be used to represent decimals, fractions, scientific notation, etc. that need to be accurately represented. Real numbers, unlike integer types, floating point numbers can represent numbers after the decimal point, and can perform four arithmetic operations on decimals.

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

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:

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

Common database float lengths are: 1. The float type length in MySQL can be 4 bytes or 8 bytes; 2. The float type length in Oracle can be 4 bytes or 8 bytes; 3. , The length of the float type in SQL Server is fixed at 8 bytes; 4. The length of the float type in PostgreSQL can be 4 bytes or 8 bytes, etc.

See all articles