Home > Backend Development > PHP Tutorial > Examples of ideas for improving program operation efficiency in PHP programming_PHP tutorial

Examples of ideas for improving program operation efficiency in PHP programming_PHP tutorial

WBOY
Release: 2016-07-13 17:42:39
Original
779 people have browsed it

As you can see from here, the second piece of code will continuously make judgments with a number of 1 2 3 according to the number of keys in the array, so the number of judgments in the first piece of code is 3, and the number of judgments in the second piece of code is 3 The number of code segment judgments is 6 times

It has been 4 years since I switched from .NET to PHP, and recently I have begun to pursue high performance~~
So I started to think it is time to write a blog~
Come to segment Find something first~
$arr = array(
attr1 => 1 ,
attr2 => 1 ,
attr3 => 1 ,
) ;
$startTime = microtime( true );
for( $i = 0 ; $i < 1000 ; $i )
{
if( isset( $arr[attr1] ) )
{
}
if( isset( $arr[attr2] ) )
{
}
if( isset( $arr[attr3] ) )
{
}
}
$endTime = microtime( true );
printf( "%d us. " , ( $endTime - $startTime ) * 1000000 );
$startTime = microtime( true );
for( $i = 0 ; $i < 1000 ; $i )
{
foreach( $arr as $key => $value )
{
switch( $key )
{
case attr1:
break;
case attr2:
break ;
case attr3:
break;
}
}
}
$endTime = microtime( true );
printf( "%d us. " , ( $endTime - $startTime ) * 1000000 );
?>
The output result of the above code
is
us.
us.
However, no matter how you look at it, The first paragraph is more cumbersome than the second paragraph, and the structure is not as clear as the second paragraph.
So why does the first paragraph execute so much faster than the second paragraph?
We can see that in the first paragraph of code, There are only 3 ifs,
So how many will there be in the second paragraph?
We have disassembled the switch, and we can take a look at its basic implementation principle.
If in switch, in each case. If they are all ended with break;,
In fact, this switch is like multiple if{}else if{}

So from this mechanism, we can put
foreach( $arr as $key => $value )
{
switch( $key )
{
case attr1:
break;
case attr2:
break ;
case attr3:
break;
}
}
?>
convert to
foreach( $arr as $key => ; $value )
{
if( $key == attr1 )
{

}
else if( $key == attr2 )
{

}
else if( $key == attr3 )
{

}
}
?>
To understand, you can see from here, Chapter The second piece of code will continuously make judgments with a number of 1 2 3 based on the number of keys in the array, so the number of judgments in the first piece of code is 3, and the number of judgments in the second piece of code is 6

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486047.htmlTechArticleAs you can see from here, the second piece of code will continue to function according to the number of keys in the array. The number of judgments is 1 2 3, so the number of judgments in the first piece of code is 3, and the second...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template