note 125878 added to function.abs
| From: | eep2004 at ukr dot net | Date: | Wed, 03 Mar 2021 08:19:45 +0000 |
| Subject: | note 125878 added to function.abs | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219126@lists.php.net to get a copy of this message | ||
<?php
echo 'PHP '.PHP_VERSION.'<br>';
$qty = 1000;
$arr = array();
for ($i = 0; $i < $qty; $i++){
$arr[] = rand(-100, 100);
}
$start = microtime(true);
for ($i = 0; $i < $qty; $i++){
foreach ($arr as $v){
$v = abs($v);
}
}
echo number_format(microtime(true) - $start, 4).'<br>';
$start = microtime(true);
for ($i = 0; $i < $qty; $i++){
foreach ($arr as $v){
if ($v < 0) $v = abs($v);
}
}
echo number_format(microtime(true) - $start, 4).'<br>';
$start = microtime(true);
for ($i = 0; $i < $qty; $i++){
foreach ($arr as $v){
if ($v < 0) $v *= -1;
}
}
echo number_format(microtime(true) - $start, 4).'<br>';
?>
Result:
PHP 7.1.33
0.0910
0.0710
0.0550
Conclusion: better to check before using the feature that the number is less than zero. Even better
use multiplication by -1 than this function.
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.245 (proxied: 194.12.65.233)
----
Manual Page -- http://php.net/manual/en/function.abs.php
Edit -- https://master.php.net/note/edit/125878
Del: integrated -- https://master.php.net/note/delete/125878/integrated
Del: useless -- https://master.php.net/note/delete/125878/useless
Del: bad code -- https://master.php.net/note/delete/125878/bad+code
Del: spam -- https://master.php.net/note/delete/125878/spam
Del: non-english -- https://master.php.net/note/delete/125878/non-english
Del: in docs -- https://master.php.net/note/delete/125878/in+docs
Del: other reasons-- https://master.php.net/note/delete/125878
Reject -- https://master.php.net/note/reject/125878
Search -- https://master.php.net/manage/user-notes.php