note 119645 added to function.abs
| From: | eep2004 at ukr dot net | Date: | Mon, 25 Jul 2016 09:28:20 +0000 |
| Subject: | note 119645 added to function.abs | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-206095@lists.php.net to get a copy of this message | ||
<?php
$arr = array();
for ($i = 0; $i < 1000; $i++) $arr[] = rand(-100, 100);
$start = microtime(true);
for ($i = 0; $i < 1000; $i++){
foreach ($arr as $v) $v = abs($v);
}
echo number_format(microtime(true) - $start, 4).'<br />';
$start = microtime(true);
for ($i = 0; $i < 1000; $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 < 1000; $i++){
foreach ($arr as $v) if ($v < 0) $v *= -1;
}
echo number_format(microtime(true) - $start, 4).'<br />';
?>
Result:
1.4061
0.9697
0.2805
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: 72.52.91.14
Probable Submitter: 194.12.65.233
----
Manual Page -- http://php.net/manual/en/function.abs.php
Edit -- https://master.php.net/note/edit/119645
Del: integrated -- https://master.php.net/note/delete/119645/integrated
Del: useless -- https://master.php.net/note/delete/119645/useless
Del: bad code -- https://master.php.net/note/delete/119645/bad+code
Del: spam -- https://master.php.net/note/delete/119645/spam
Del: non-english -- https://master.php.net/note/delete/119645/non-english
Del: in docs -- https://master.php.net/note/delete/119645/in+docs
Del: other reasons-- https://master.php.net/note/delete/119645
Reject -- https://master.php.net/note/reject/119645
Search -- https://master.php.net/manage/user-notes.php