Home >Backend Development >PHP Tutorial >php high performance writing
It has been 4 years since I switched from .NET to PHP, and recently I started to pursue high performance~~
So I started to think it’s time to write a blog~
Let’s discover something first~
Copy the code The code is as follows:
$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.n" , ( $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.n" , ( $endTime - $startTime ) * 1000000 );
The code is as follows:
foreach( $arr as $key => $value ){
switch( $key )
{
case 'attr1':
break;
case 'attr2':
break;
case ' attr3':
break;
}
}
converted to
Copy code The code is as follows:
foreach( $arr as $key => $value ){
if( $key = = 'attr1' )
{
}
else if( $key == 'attr2' )
{
}
else if( $key == 'attr3' )
{
}
}
to understand,