The foreach and while loops in PHP, so what's the difference between the foreach and the While loop, and that performance will be better, let me introduce you to the foreach and while loop difference and performance comparison, there is a need to understand the students can refer to.
In the while loop, Perl reads a line of input, puts it into a variable, and executes the loop body. Then it goes back and looks for the other input lines.
In a foreach loop, the entire line of input operators executes in the context of the list (because foreach needs to process the contents of the list line by row). Before the loop starts executing, it must first read all the input.
When you enter a large file, using foreach consumes a lot of memory. The difference between the two will be very obvious. Therefore, the best practice is to use the shorthand of the while loop as much as possible so that it processes one line at a time.
Here are some information:
C # provides 4 different loop statements for you to use when you want to repeat certain statements or segments, depending on the current task:
。 For statement
。 foreach statement
。 While statement
。 Do statement
1.for
The For statement is especially useful when you know in advance how many times an embedded statement should be executed. When the condition is true, the regular syntax allows for repeated execution of the included statement (and loop expression):
for (initialize; condition; loop) contains statements
Note that initialization, conditions, and loops are optional. If you omit the condition, you can create a dead loop that uses a jump statement (break or Goto) to exit.
| The code is as follows |
Copy Code |
|
for (;;)
{
Break For some reason
} |
Another important point is that you can add more than one comma-delimited statement to all three parameters of the For loop. For example, you can initialize two variables, have three conditional statements, and repeat 4 variables.
2.foreach
A feature that has existed in the visual Basic language for a long time is to collect enumerations by using the for each statement. C # through the foreach statement, there is also a command to collect enumerations:
foreach (type identifier in an expression) contains statements
A loop variable is declared by a type and an identifier, and the expression corresponds to the collection. The loop variable represents the collection element that the loop is running for.
3.while
When you want to execute an embedded statement 0 or more times, the while statement is exactly what you expect:
while (conditional) contains statements
The conditional statement-which is also a Boolean expression-controls the number of times the containing statement is executed. You can use the break and continue statements to control the execution statement in a while statement, which runs exactly the same way as in a for statement.
4,do
The last available loop statement for C # is the Do statement. It is very similar to the while statement, and the condition is validated only after the initial loop.
| The code is as follows |
Copy Code |
|
Do
{
Included statements
}
while (condition); |
The Do statement guarantees that the included statements are executed at least once, and they continue to be executed as long as the condition evaluates to True. By using the break statement, you can force the run to exit the Do block. If you want to skip this cycle, use the Continue statement.
Performance comparison
foreach is the operation of an array copy (by copying the arrays), while the while is manipulated by moving the internal indicators of the array, which is generally considered to be faster than foreach (because the foreach first copies the array in the beginning of execution). While the while moves the internal indicator directly. ), but the result is just the opposite.
In the loop is the array "read" operation, then the foreach is faster than the while:
| The code is as follows |
Copy Code |
foreach ($array as $value) {
Echo $value;
}
while (list ($key) = each ($array)) {
echo $array [$key];
} |
In the loop is an array of "write" operations, while the while is faster than foreach:
| The code is as follows |
Copy Code |
foreach ($array as $key = = $value) {
echo $array [$key] = $value. '...';
}
while (list ($key) = each ($array)) {
$array [$key] = $array [$key]. '...';
} |
Let's test the time taken together to traverse a one-dimensional array with 50,000 subscripts:
Test Platform:
CPU:P-M 725
Memory: 512M
HDD: 40G 5400 rpm
Os:windows XP SP2
Web:apache 2.0.54 php5.0.4
Test code:
| The code is as follows |
Copy Code |
|
/*
* @ Author:lilov
* @ Homepage:www.bKjia.c0m
* @ e-mail:zhongjiechao@gmail.com
*
*/
$arr = Array ();
for ($i = 0; $i < 50000; $i + +) {
$arr [] = $i *rand (1000,9999);
}
function GetRuntime ()
{
List ($usec, $sec) =exp Lode ("", Microtime ());
return (float) $usec + (float) $sec);
}
######################################
$time _start = GetRuntime ();
for ($i = 0; $i < count ($arr); $i + +) {
$str. = $arr [$i];
}
$time _end = GetRuntime ();
$time _used = $time _end-$time _start;
Echo ' used time of For: '. Round ($time _used, 7). ' (s)
';
Unset ($str, $time _start, $time _end, $time _used);
######################################
$time _start = GetRuntime ();
while (list ($key, $val) = each ($arr)) {
$str. = $val;
}
$time _end = GetRuntime ();
$time _used = $time _end-$time _start;
Echo ' used time of while: '. Round ($time _used, 7). ' (s)
';
Unset ($str, $key, $val, $time _start, $time _end, $time _used);
######################################
$time _start = GetRuntime ();
foreach ($arr as $key = = $val) {
$str. = $val;
}
$time _end = GetRuntime ();
$time _used = $time _end-$time _start;
Echo ' Used time of foreach: '. Round ($time _used, 7). ' (s)
';
######################################
?>
|
Test results:
Averaging three test results:
Corresponds to For, while, foreach, respectively
0.1311650
0.1666853
0.1237440
After repeated tests, the result shows that for traversing the same array, the foreach speed is the fastest and the slowest is the while. The foreach is about 20% ~ 30% faster than while. The array subscript is then added to the 500000 and 5000000 test results. In principle, however, foreach is an array copy operation (by copying the arrays), while the while is performed by moving the internal indicator of the array, and the general logic is that the while should be faster than foreach (because the foreach first copies the array in the beginning of execution). While the while moves the internal indicator directly. ), but the result is just the opposite. It should be that foreach is an internal implementation of PHP, while the while is a common loop structure.
Summary: It is generally assumed that foreach involves a value copy, which must be slower than while, but in fact, if the array is read only in the loop, then foreach is very fast, because PHP uses a copy mechanism that is "reference count, copy on write", which means , even if you copy a variable in PHP, the initial form is essentially still the form of a reference, and only if the content of the variable changes, the actual replication occurs, and the reason for doing so is to save the memory consumption and also improve the efficiency of replication. Thus, the efficient read operation of foreach is not difficult to understand. In addition, since foreach is not suitable for handling array writes, we can draw a conclusion that in most cases, the code for array write operations like foreach ($array as $key + $value) should be replaced by the while (List ($key ) = each ($array)). The speed differences generated by these techniques may not be obvious in small projects, but in large projects such as frameworks, a single request can easily involve hundreds of thousands of tens of thousands of array loops, and the difference will be magnified significantly.
http://www.bkjia.com/PHPjc/628743.html www.bkjia.com true http://www.bkjia.com/PHPjc/628743.html techarticle The foreach and while loops in PHP, so what's the difference between the foreach and the While loop, and that performance is better, let me show you the foreach and while loop differences and ...