Skip to content

Instantly share code, notes, and snippets.

@kalimatas
Last active May 26, 2020 13:02
Show Gist options
  • Save kalimatas/6e8d3d455443922820074396329cfadb to your computer and use it in GitHub Desktop.
Save kalimatas/6e8d3d455443922820074396329cfadb to your computer and use it in GitHub Desktop.

Revisions

  1. Alexander Guz revised this gist Dec 13, 2016. 1 changed file with 15 additions and 14 deletions.
    29 changes: 15 additions & 14 deletions PushVsMerge.php
    Original file line number Diff line number Diff line change
    @@ -2,41 +2,42 @@

    declare(strict_types = 1);

    use PhpBench\Benchmark\Metadata\Annotations\BeforeMethods;
    use PhpBench\Benchmark\Metadata\Annotations\Revs;

    require 'vendor/autoload.php';

    /**
    * @BeforeMethods({"prepare"})
    */
    class PushVsMergeBench
    {
    private function prepare()
    private $a = [];
    private $b = [];

    public function prepare()
    {
    $a = [];
    $b = [];
    $this->a = [];
    $this->b = [];
    for ($i = 0; $i < 100000; $i++) {
    $a[] = new \stdClass();
    $b[] = new \stdClass();
    $this->a[] = new \stdClass();
    $this->b[] = new \stdClass();
    }

    return [$a, $b];
    }

    /**
    * @Revs(100)
    */
    public function benchArrayMerge()
    {
    list($a, $b) = $this->prepare();

    $a = array_merge($a, $b);
    $this->a = array_merge($this->a, $this->b);
    }

    /**
    * @Revs(100)
    */
    public function benchArrayPush()
    {
    list($a, $b) = $this->prepare();

    array_push($a, ...$b);
    array_push($this->a, ...$this->b);
    }
    }
    }
  2. Alexander Guz created this gist Dec 13, 2016.
    42 changes: 42 additions & 0 deletions PushVsMerge.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    <?php

    declare(strict_types = 1);

    use PhpBench\Benchmark\Metadata\Annotations\Revs;

    require 'vendor/autoload.php';

    class PushVsMergeBench
    {
    private function prepare()
    {
    $a = [];
    $b = [];
    for ($i = 0; $i < 100000; $i++) {
    $a[] = new \stdClass();
    $b[] = new \stdClass();
    }

    return [$a, $b];
    }

    /**
    * @Revs(100)
    */
    public function benchArrayMerge()
    {
    list($a, $b) = $this->prepare();

    $a = array_merge($a, $b);
    }

    /**
    * @Revs(100)
    */
    public function benchArrayPush()
    {
    list($a, $b) = $this->prepare();

    array_push($a, ...$b);
    }
    }