Skip to content

Instantly share code, notes, and snippets.

@Majkl578
Created April 11, 2018 01:37
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 Majkl578/d3f88d28b1c6b814f730f937c5746f9f to your computer and use it in GitHub Desktop.
Save Majkl578/d3f88d28b1c6b814f730f937c5746f9f to your computer and use it in GitHub Desktop.
signature misuse + func_get_args() vs. assert() + count() @ PHP 7.2
<?php
function foo(int ...$prices)
{
assert(count($prices) > 0);
}
$t = microtime(true);
for ($i = 0; $i < 10000000; $i++) {
foo(1, 2, 3, 4, 5);
}
var_dump(microtime(true) - $t); // float(0.87249183654785)
<?php
function foo(int $price, int ...$prices)
{
$prices = func_get_args();
}
$t = microtime(true);
for ($i = 0; $i < 10000000; $i++) {
foo(1, 2, 3, 4, 5);
}
var_dump(microtime(true) - $t); //float(0.99412393569946)
@rybakit
Copy link

rybakit commented Apr 11, 2018

@Majkl578

// assert.php
<?php

function foo(int ...$prices)
{
    assert(count($prices) > 0);
    foreach ($prices as $price) {
        random_int(0, $price);
    }
}

$t = microtime(true);

for ($i = 0; $i < 1000000; $i++) {
    foo(1, 2, 3, 4, 5);
}

var_dump(microtime(true) - $t);
// variadic.php
<?php

function foo(int $price, int ...$prices)
{
    foreach ($prices as $price) {
        random_int(0, $price);
    }
}

$t = microtime(true);

for ($i = 0; $i < 1000000; $i++) {
    foo(1, 2, 3, 4, 5);
}

var_dump(microtime(true) - $t);
$ php -n -dzend.assertions=-1 assert.php                    
float(3.1659638881683)                 
$ php -n variadic.php                                       
float(2.5708408355713)
$ php -v            
PHP 7.1.16 (cli) (built: Mar 28 2018 07:11:55) ( NTS )

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