Small comparison of PHP function execution efficiency

Source: Internet
Author: User
Write a custom function to make $ dataarray (,) & gt; $ dataarray ); the number in the original array is split into "single" bits.
The following is a self-written function:

The code is as follows:


Function splitStrToArray_mine ($ array)
{
$ New_array = array ();
Foreach ($ array as $ value)
{
$ Value = (string) $ value;
$ Len = strlen ($ value );
For ($ I = 0; $ I <$ len; $ I ++ ){
Array_push ($ new_array, $ value {$ I });
}
}
Return $ new_array;
}


After testing, you can still execute the following calls:

The code is as follows:


// Test the array
$ Data = array (12, 43, 87, 45, 98, 74, 83, 67, 12 );
Var_dump (splitStrToArray_mine ($ data ));


Output result:

The code is as follows:


Array (18 ){
[0] =>
String (1) "1"
[1] =>
String (1) "2"
[2] =>
String (1) "4"
[3] =>
String (1) "3"
[4] =>
String (1) "8"
[5] =>
String (1) "7"
[6] =>
String (1) "4"
[7] =>
String (1) "5"
[8] =>
String (1) "9"
[9] =>
String (1) "8"
[10] =>
String (1) "7"
[11] =>
String (1) "4"
[12] =>
String (1) "8"
[13] =>
String (1) "3"
[14] =>
String (1) "6"
[15] =>
String (1) "7"
[16] =>
String (1) "1"
[17] =>
String (1) "2"
}


Although the execution is good, looking at the standard answer will surprise you. the following is a sentence in the function:

The code is as follows:


// Standard functions
Function splitStrToArray ($ array)
{
Return str_split (implode ("", $ array ));
}


Therefore, I wrote a script to test the running efficiency gap between my function and the standard function. there is a microtime_float () function in it to provide support for accurate time:

The code is as follows:


// Function for measuring time
Function microtime_float ()
{
List ($ usec, $ sec) = explode ("", microtime ());
Return (float) $ usec + (float) $ sec );
}
// User-defined functions
Function splitStrToArray_mine ($ array)
{
$ New_array = array ();
Foreach ($ array as $ value)
{
$ Value = (string) $ value;
$ Len = strlen ($ value );
For ($ I = 0; $ I <$ len; $ I ++ ){
Array_push ($ new_array, $ value {$ I });
}
}
Return $ new_array;
}
// Standard functions
Function splitStrToArray ($ array)
{
Return str_split (implode ("", $ array ));
}
// Test the array
$ Data = array (12, 43, 87, 45, 98, 74, 83, 67, 12 );
// Start the test
$ Mine_start = microtime_float ();
SplitStrToArray_mine ($ data );
$ Mine_end = microtime_float ();
// Calls Standard functions
$ Sta_start = microtime_float ();
SplitStrToArray ($ data );
$ Sta_end = microtime_float ();
Echo "the running time of function calls is:". (float) ($ mine_end-$ mine_start). "S
";
Echo "standard function call runtime:". (float) ($ sta_end-$ sta_start). "S
";
$ Multiple = (int) (float) ($ mine_end-$ mine_start)/(float) ($ sta_end-$ sta_start ));
Echo "the former is the latter:". $ multiple. "times! ";


Let's take a look at the output:
The running time of your function call is 9.3920.20166e-005 S.
Standard function call Run time: 2.69412994425e-005 S
The former is 3 times the latter!
If you refresh the page multiple times, you can find that the execution efficiency of standard functions is basically three times that of your own functions! Of course, the standard functions use PHP built-in functions: str_split () and implode (), so it is much faster than writing a function by yourself, and I have no idea about str_split () functions? Let's take a look at the manual explanation:
Str_split -- Convert a string to an array (Convert a string to an array)
Function description:
Array str_split (string [, int split_length])

The code is as follows:


Converts a string to an array. if the optional split_length parameter is specified, the returned array will be broken down into chunks with each being split_length in length, otherwise each chunk will be one character in length.
FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string, the entire string is returned as the first (and only) array element.


Example 1. Example uses of str_split ()

The code is as follows:


$ Str = "Hello Friend ";
$ Arr1 = str_split ($ str );
$ Arr2 = str_split ($ str, 3 );
Print_r ($ arr1 );
Print_r ($ arr2 );
?>


Output may look like:

The code is as follows:


Array
(
[0] => H
[1] => e
[2] => l
[3] => l
[4] => o
[5] =>
[6] => F
[7] => r
[8] => I
[9] => e
[10] => n
[11] => d
)
Array
(
[0] => El
[1] => lo
[2] => Fri
[3] => end
)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.