Last active
May 23, 2019 10:05
-
-
Save janw-me/7b2c9a5a929db60098ec3727f7ff8656 to your computer and use it in GitHub Desktop.
Foreach & for performance test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo '<pre>'; | |
$test_array = array(); | |
for ( $i = 0; $i <= 10000; $i ++ ) { | |
$test_array[] = [ | |
'index' => $i, | |
'include' => ( $i % 3 === 0 ) ? true : false, | |
]; | |
} | |
$start = microtime( true ); | |
$test_function = function ( $test_array ) { | |
$filtered_array = []; | |
foreach ( $test_array as $item ) { | |
if ( true === $item['include'] ) { | |
$filtered_array[] = $item; | |
} | |
} | |
return $filtered_array; | |
}; | |
for ( $i = 0; $i <= 5000; $i ++ ) { | |
call_user_func( $test_function, $test_array ); | |
} | |
echo microtime( true ) - $start . "sec Loop: foreach traditional<br/>"; | |
$start = microtime( true ); | |
$test_function = function ( $test_array ) { | |
foreach ( $test_array as $key => $item ) { | |
if ( true === $item['include'] ) { | |
unset( $test_array[ $key ] ); | |
} | |
} | |
return $test_array; | |
}; | |
for ( $i = 0; $i <= 5000; $i ++ ) { | |
call_user_func( $test_function, $test_array ); | |
} | |
echo microtime( true ) - $start . "sec Loop: foreach unset key<br/>"; | |
$start = microtime( true ); | |
$test_function = function ( $test_array ) { | |
foreach ( $test_array as $key => &$item ) { | |
if ( true === $item['include'] ) { | |
unset( $test_array[ $key ] ); | |
} | |
} | |
return $test_array; | |
}; | |
for ( $i = 0; $i <= 5000; $i ++ ) { | |
call_user_func( $test_function, $test_array ); | |
} | |
echo microtime( true ) - $start . "sec Loop: foreach &unset key<br/>"; | |
$start = microtime( true ); | |
$test_function = function ( $test_array ) { | |
$filtered_array = []; | |
foreach ( $test_array as &$item ) { | |
if ( true === $item['include'] ) { | |
$filtered_array[] = $item; | |
} | |
} | |
return $filtered_array; | |
}; | |
for ( $i = 0; $i <= 5000; $i ++ ) { | |
call_user_func( $test_function, $test_array ); | |
} | |
echo microtime( true ) - $start . "sec Loop: foreach &traditional<br/>"; | |
$start = microtime( true ); | |
$test_function = function ( $test_array ) { | |
$length = count( $test_array ); | |
for ( $i = 0; $i < $length; $i ++ ) { | |
if ( true === $test_array[ $i ]['include'] ) { | |
unset( $test_array[ $i ] ); | |
} | |
} | |
return $test_array; | |
}; | |
for ( $i = 0; $i <= 5000; $i ++ ) { | |
call_user_func( $test_function, $test_array ); | |
} | |
echo microtime( true ) - $start . "sec Loop: for<br/>"; | |
echo '</pre>'; | |
var_dump( phpversion() ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
echo '<pre>'; | |
$test_array = array(); | |
for ( $i = 0; $i <= 10000; $i ++ ) { | |
$test_array[] = [ | |
'index' => $i, | |
'include' => ( $i % 3 === 0 ) ? true : false, | |
]; | |
} | |
$start = microtime( true ); | |
$test_function = function ( $test_array ) { | |
$filtered_array = []; | |
foreach ( $test_array as $item ) { | |
if ( true === $item['include'] ) { | |
$item['index'] ++; | |
} | |
$filtered_array[] = $item; | |
} | |
return $filtered_array; | |
}; | |
for ( $i = 0; $i <= 7500; $i ++ ) { | |
call_user_func( $test_function, $test_array ); | |
} | |
echo microtime( true ) - $start . "sec Loop: foreach traditional<br/>"; | |
$start = microtime( true ); | |
$test_function = function ( $test_array ) { | |
foreach ( $test_array as $key => $item ) { | |
if ( true === $item['include'] ) { | |
$test_array[ $key ]['index'] ++; | |
} | |
} | |
return $test_array; | |
}; | |
for ( $i = 0; $i <= 7500; $i ++ ) { | |
call_user_func( $test_function, $test_array ); | |
} | |
echo microtime( true ) - $start . "sec Loop: foreach unset key<br/>"; | |
$start = microtime( true ); | |
$test_function = function ( $test_array ) { | |
foreach ( $test_array as $key => &$item ) { | |
if ( true === $item['include'] ) { | |
$item['index'] ++; | |
} | |
} | |
return $test_array; | |
}; | |
for ( $i = 0; $i <= 7500; $i ++ ) { | |
call_user_func( $test_function, $test_array ); | |
} | |
echo microtime( true ) - $start . "sec Loop: foreach traditional by reference<br/>"; | |
$start = microtime( true ); | |
$test_function = function ( $test_array ) { | |
$length = count( $test_array ); | |
for ( $i = 0; $i < $length; $i ++ ) { | |
if ( true === $test_array[ $i ]['include'] ) { | |
$test_array[ $i ]['index'] ++; | |
} | |
} | |
return $test_array; | |
}; | |
for ( $i = 0; $i <= 7500; $i ++ ) { | |
call_user_func( $test_function, $test_array ); | |
} | |
echo microtime( true ) - $start . "sec Loop: for<br/>"; | |
echo '</pre>'; | |
var_dump( phpversion() ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment