Skip to content

Instantly share code, notes, and snippets.

@macocci7
Last active December 17, 2023 12:10
Show Gist options
  • Save macocci7/eee86e1c5b6982edbe578dd3f38f3714 to your computer and use it in GitHub Desktop.
Save macocci7/eee86e1c5b6982edbe578dd3f38f3714 to your computer and use it in GitHub Desktop.
Script to benchmark the performance of adding array elements (配列要素追加処理のベンチマークスクリプト)
<?php
function benchmark(string $name, Closure $callback)
{
$start = microtime(true);
$callback();
$time = microtime(true) - $start;
echo sprintf(
"%24s =>\tTime: %.6f sec.\n",
$name,
$time
);
}
$iteration = 10000;
// array_push()
benchmark(
'array_push()',
function () use ($iteration) {
$a = [];
for ($i = 0; $i < $iteration; $i++) {
array_push($a, 1);
}
}
);
// square brackets
benchmark(
'square brackets []',
function () use ($iteration) {
$a = [];
for ($i = 0; $i < $iteration; $i++) {
$a[] = 1;
}
}
);
// union operator
benchmark(
'union operator +=',
function () use ($iteration) {
$a = [];
for ($i = 0; $i < $iteration; $i++) {
$a += [count($a) => 1];
}
}
);
// array_merge()
benchmark(
'array_merge()',
function () use ($iteration) {
$a = [];
for ($i = 0; $i < $iteration; $i++) {
$a = array_merge($a, [1]);
}
}
);
// unpacking operator
benchmark(
'unpacking operator [...]',
function () use ($iteration) {
$a = [];
for ($i = 0; $i < $iteration; $i++) {
$a = [...$a, 1];
}
}
);
// ArrayObject
benchmark(
'ArrayObject',
function () use ($iteration) {
$a = new ArrayObject([]);
for ($i = 0; $i < $iteration; $i++) {
$a->append(1);
}
}
);
@macocci7
Copy link
Author

macocci7 commented Dec 17, 2023

Result of running 5 times:

  • 1st:

                array_push() =>	Time: 0.004728 sec.
          square brackets [] =>	Time: 0.002071 sec.
          union operator += =>	Time: 0.003370 sec.
              array_merge() =>	Time: 0.105991 sec.
    unpacking operator [...] =>	Time: 0.311016 sec.
                ArrayObject =>	Time: 0.006851 sec.
    
  • 2nd:

                array_push() =>	Time: 0.004765 sec.
          square brackets [] =>	Time: 0.002110 sec.
          union operator += =>	Time: 0.003569 sec.
              array_merge() =>	Time: 0.107618 sec.
    unpacking operator [...] =>	Time: 0.330064 sec.
                ArrayObject =>	Time: 0.004905 sec.
    
  • 3rd:

                array_push() =>	Time: 0.004920 sec.
          square brackets [] =>	Time: 0.002054 sec.
          union operator += =>	Time: 0.003514 sec.
              array_merge() =>	Time: 0.113861 sec.
    unpacking operator [...] =>	Time: 0.300871 sec.
                ArrayObject =>	Time: 0.004863 sec.
    
  • 4th:

                array_push() =>	Time: 0.005846 sec.
          square brackets [] =>	Time: 0.002054 sec.
          union operator += =>	Time: 0.003339 sec.
              array_merge() =>	Time: 0.106517 sec.
    unpacking operator [...] =>	Time: 0.360666 sec.
                ArrayObject =>	Time: 0.005932 sec.
    
  • 5th:

                array_push() =>	Time: 0.005791 sec.
          square brackets [] =>	Time: 0.002138 sec.
          union operator += =>	Time: 0.004024 sec.
              array_merge() =>	Time: 0.118922 sec.
    unpacking operator [...] =>	Time: 0.393219 sec.
                ArrayObject =>	Time: 0.005309 sec.
    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment