note 78442 added to function.return
| From: | mr dot xanadu at gmail dot com | Date: | Fri, 12 Oct 2007 08:56:27 +0000 |
| Subject: | note 78442 added to function.return | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131329@lists.php.net to get a copy of this message | ||
I was wondering what was quicker:
- return a boolean as soon I know it's value ('direct') or
- save the boolean in a variable and return it at the function's end.
<?php
$times = 50000;
function return_direct ($boolean)
{
if ($boolean == true)
{
return true;
}
return false;
}
function return_indirect ($boolean)
{
$return = false;
if ($boolean == true)
{
$return = true;
}
return $return;
}
/* Direct, return true */
$time_start = microtime(true);
for ($i = 1; $i <= $times; $i++)
{
return_direct(true);
}
$time_end = microtime(true);
$time_direct_true = $time_end - $time_start;
/* Direct, return false */
$time_start = microtime(true);
for ($i = 1; $i <= $times; $i++)
{
return_direct(false);
}
$time_end = microtime(true);
$time_direct_false = $time_end - $time_start;
/* Indirect, return true */
$time_start = microtime(true);
for ($i = 1; $i <= $times; $i++)
{
return_indirect(true);
}
$time_end = microtime(true);
$time_indirect_true = $time_end - $time_start;
/* Direct, return false */
$time_start = microtime(true);
for ($i = 1; $i <= $times; $i++)
{
return_indirect(false);
}
$time_end = microtime(true);
$time_indirect_false = $time_end - $time_start;
echo "<pre>";
echo "direct true\t" . $time_direct_true;
echo "\ndirect false\t" . $time_direct_false;
echo "\nindirect true\t" . $time_indirect_true;
echo "\nindirect false\t" . $time_indirect_false;
echo "<pre>";
?>
Representative results:
direct true 0.163973093033
direct false 0.1270840168
indirect true 0.0733940601349
indirect false 0.0742440223694
Conclusion: saving the result in a variable appears to be faster. (Please note that my test
functions are very simple, maybe it's slower on longer functions)
----
Server IP: 83.137.20.107
Probable Submitter: 82.92.195.210
----
Manual Page -- http://www.php.net/manual/en/function.return.php
Edit -- https://master.php.net/note/edit/78442
Del: integrated -- https://master.php.net/note/delete/78442/integrated
Del: useless -- https://master.php.net/note/delete/78442/useless
Del: bad code -- https://master.php.net/note/delete/78442/bad+code
Del: spam -- https://master.php.net/note/delete/78442/spam
Del: non-english -- https://master.php.net/note/delete/78442/non-english
Del: in docs -- https://master.php.net/note/delete/78442/in+docs
Del: other reasons-- https://master.php.net/note/delete/78442
Reject -- https://master.php.net/note/reject/78442
Search -- https://master.php.net/manage/user-notes.php