A small comparison of the efficiency of PHP function execution

Source: Internet
Author: User
Tags foreach comparison copy execution functions return strlen
is to "split" the numbers in the original array into "single" bits.
Here is a function written by yourself:
Copy CodeThe 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;
}

Test, or can be performed, such as downward use:
Copy CodeThe code is as follows:
Test array
$data = Array (12, 43, 87, 45, 98, 74, 83, 67, 12);
Var_dump (Splitstrtoarray_mine ($data));

The output results are:
Copy CodeThe 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 implementation is good, but look at the standard answer will let you surprised, the function of a word, as follows:
Copy CodeThe code is as follows:
Standard functions
function Splitstrtoarray ($array)
{
Return Str_split (Implode ("", $array));
}

Then wrote the script to test their own and standard functions of the operational efficiency gap, which has a microtime_float () function to provide accurate time support:
Copy CodeThe code is as follows:
function to measure time
function Microtime_float ()
{
List ($usec, $sec) = Explode ("", Microtime ());
Return ((float) $usec + (float) $sec);
}
Custom 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 array
$data = Array (12, 43, 87, 45, 98, 74, 83, 67, 12);
Start testing
$mine _start = Microtime_float ();
Splitstrtoarray_mine ($data);
$mine _end = Microtime_float ();
Standard function call
$sta _start = Microtime_float ();
Splitstrtoarray ($data);
$sta _end = Microtime_float ();
echo "'s own function call run time is:". (float) ($mine _end-$mine _start). "S <br/>";
echo "Standard function call run time is:". (float) ($sta _end-$sta _start). "S <br/>";
$multiple = (int) (float) ($mine _end-$mine _start)/(float) ($sta _end-$sta _start));
Echo "The former is the latter:". $multiple. Times ";

to see the output:
Its own function call run time is: 9.3936920166E-005 S
Standard function call run time: 2.69412994385E-005 S
The former is the latter: 3 times times!
Refresh the page more than once, you can find that the performance of the standard function is basically its own function of 3 times times! Of course, the standard function uses PHP's built-in function: Str_split (), implode (), so it's much faster than writing a function, and the Str_split () function has no impression? Look at the manual to explain:
Str_split-Convert a string to a array (converts a string to a group)
Function Description:
Array Str_split (string string [, int split_length])
Copy CodeThe code is as follows:
Converts a string to a array. If The optional split_length parameter is specified, the returned array would be broken down to chunks with each being SP Lit_length in length, otherwise each chunk would 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 as the "array Eleme Nt.

Example 1. Example uses of Str_split ()
Copy CodeThe code is as follows:
<?php
$str = "Hello Friend";
$arr 1 = str_split ($STR);
$arr 2 = Str_split ($STR, 3);
Print_r ($arr 1);
Print_r ($arr 2);
?>

Output may look like:
Copy CodeThe code is as follows:
Array
(
[0] => H
[1] => E
[2] => L
[3] => L
[4] => O
[5] =>
[6] => F
[7] => R
[8] => I
[9] => E
[Ten] => n
[One] => D
)
Array
(
[0] => Hel
[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.