Skip to content

Instantly share code, notes, and snippets.

@sad270
Created November 19, 2021 13:23
Show Gist options
  • Save sad270/97c3701344f2a1e831d67a394a78170f to your computer and use it in GitHub Desktop.
Save sad270/97c3701344f2a1e831d67a394a78170f to your computer and use it in GitHub Desktop.
<?php
/*
MIT License
Copyright (c) 2021 Sadetdin EYILI
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/**
* How to use ?
* run `php bench.php -i 4000` for 4000 iterations
*/
// get Iteration
$iteration = 100;
$opts = getopt('i:');
if(count($opts) > 0 && isset($opts['i'])) {
if (is_array($opts['i'])) {
$opts['i'] = $opts['i'][0];
}
$iteration = (int) $opts['i'];
}
if ($iteration <= 0) {
echo 'Interation must be greater than 0'. PHP_EOL;
exit(1);
}
echo $iteration.' iteration(s)' . PHP_EOL;
// Create a function like array_push with `$array[] =` to bench it
function my_array_push(array &$array, ...$values): int {
foreach($values as $value) {
$array[] = $value;
}
return count($array);
}
// Create values array for benches
$values = range(0, $iteration);
/********************
* START BENCHMARKS *
********************/
// BENCH Multiple $array[]
$t = microtime(true);
$array = array();
for($i = 0; $i < $iteration; $i++) {
$array[] = $i;
}
echo microtime(true) - $t . ' Multiple $array[]' . PHP_EOL;
// BENCH Multiple array_push()
$t = microtime(true);
$array = array();
for($i = 0; $i <= $iteration; $i++) {
array_push($array, $i);
}
echo microtime(true) - $t . ' Multiple array_push()' . PHP_EOL;
// BENCH One array_push()
$t = microtime(true);
$array = array();
array_push($array, ...$values);
echo microtime(true) - $t . ' One array_push()' . PHP_EOL;
// BENCH Multiple my_array_push()
$t = microtime(true);
$array = array();
for($i = 0; $i <= $iteration; $i++) {
my_array_push($array, $i);
}
echo microtime(true) - $t . ' Multiple my_array_push()' . PHP_EOL;
// BENCH One my_array_push()
$t = microtime(true);
$array = array();
my_array_push($array, ...$values);
echo microtime(true) - $t . ' One my_array_push()' . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment