PHP: func_get_args performance?

I am going to use func_get_argsto read additional arguments to a function call.

How does this affect performance? Should I use an array to pass extra arguments instead of reading using the above function?

+3
source share
5 answers

If you do not use it in bulk quantities, no function will have such a big difference. You can always check how long it takes to call using microtime()before and after the call, but I don’t think you will find anything interesting.

Go and use it if you want.

, , , , .

+12

, ?

, , . :

function foo($a) { }
foo(); // Warning: Missing argument 1 for foo()

function foo() { list($a) = func_get_args(); }
foo(); //no error

, foo($a), , .

func_get_args - (printf-like).

+4

, ; .

" , "

+1

, . func_get_args pov . (func_parameter), . . , .

double-quote single-quote. , PHP .

    $parameter = func_get_args();
    $parameter = $this->func_parameter("key,data,dispense=",$parameter);
    extract($parameter);
+1

, , func_get_args func_num_args, ...

php -v:

PHP 5.3.10-1ubuntu3.2 with Suhosin-Patch (cli) (built: Jun 13 2012 17:20:55) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2012 Zend Technologies
Output: 
php dynFuncArgListTest.php 
$StatTestTot: 14.194619178772
$DynTestTot: 33.246332645416

:

<?php

define('NEWLINE', "\n");

$StatTestTot = 0;
$DynTestTot = 0;

for($i = 0; $i < 9000; ++$i) {
    $randBoolArr = CreateArrayWithRandomBoolValues(900);
    $StatTestTot += StatTest($randBoolArr);
    $DynTestTot += DynTest($randBoolArr);
    $StatTestTot += StatTest($randBoolArr);
    $DynTestTot += DynTest($randBoolArr);
    $StatTestTot += StatTest($randBoolArr);
    $DynTestTot += DynTest($randBoolArr);
    $StatTestTot += StatTest($randBoolArr);
    $DynTestTot += DynTest($randBoolArr);
}

echo '$StatTestTot: ' . $StatTestTot . NEWLINE;
echo '$DynTestTot: ' . $DynTestTot . NEWLINE;

function DynTest($randBoolArr) {
    $StartDynTest = GetMicrotimeTrue();
    foreach($randBoolArr as $aBool) {
        IsBoolAndTrueDyn($aBool);
    }
    $EndDynTest = GetMicrotimeTrue();
    $Dyndisplacement = $EndDynTest - $StartDynTest;
    //echo 'Dyn Test: ' . $Dyndisplacement . NEWLINE;
    return $Dyndisplacement;
}

function StatTest($randBoolArr) {
    $StartStatTest = GetMicrotimeTrue();
    foreach($randBoolArr as $aBool) {
        IsBoolAndTrue($aBool);
    }

    $EndStatTest = GetMicrotimeTrue();
    $Statdisplacement = $EndStatTest - $StartStatTest;
    //echo 'Stat Test: ' . $Statdisplacement . NEWLINE;
    return $Statdisplacement;
}


function GetMicrotimeTrue() { return microtime(true); }

function CreateArrayWithRandomBoolValues($size) {
    $output = array();
    for($i = 0; $i < $size; ++$i) {
        $output[$i] = (rand(0, 1) === 0);
    }
    return $output;
}

function IsBoolAndTrue($input) {
    if(!is_bool($input) || $input !== true) { 
        return false;
    }
    return true;
}

function IsBoolAndTrueDyn() {
    $num_args = func_num_args();
    if($num_args > 0) {
        $args = func_get_args();
        foreach($args as $arg) {
            if(!is_bool($arg) || $arg !== true) {
                return false;
            }
        }
        return true;
    }
    return false;
}

?>
-2

Source: https://habr.com/ru/post/1760767/


All Articles