Skip to content

Instantly share code, notes, and snippets.

Created May 29, 2014 10:32
Show Gist options
  • Select an option

  • Save anonymous/aaf845ae354578b74906 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/aaf845ae354578b74906 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist May 29, 2014.
    28 changes: 28 additions & 0 deletions gistfile1.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    <?php

    function function_pass_by_value($a) {
    // Will trigger copy-on-write! WOW!
    $a[] = 101;
    }

    function function_pass_by_reference($a) {
    // Readonly. Will not trigger copy-on-write!
    // $a is passed by reference.
    $b = $a[101];
    }

    $array = array_fill(0, 100000, 123);

    $x = microtime(true);
    for ($i = 0; $i < 1000; $i++) {
    function_pass_by_reference($array);
    }
    $diff = microtime(true) - $x;
    echo count($array) . " : pass by reference : $diff\n";

    $x = microtime(true);
    for ($i = 0; $i < 1000; $i++) {
    function_pass_by_value($array);
    }
    $diff = microtime(true) - $x;
    echo count($array) . " : pass by value : $diff\n";