Skip to content

Instantly share code, notes, and snippets.

@janw-me
Last active May 23, 2019 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save janw-me/7b2c9a5a929db60098ec3727f7ff8656 to your computer and use it in GitHub Desktop.
Save janw-me/7b2c9a5a929db60098ec3727f7ff8656 to your computer and use it in GitHub Desktop.
Foreach & for performance test
<?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() );
<?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