note 119645 deleted from function.abs by salathe

From: Date: Mon, 21 Sep 2020 14:29:01 +0000
Subject: note 119645 deleted from function.abs by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-215175@lists.php.net to get a copy of this message
Note Submitter: eep2004 at ukr dot net ---- <?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.

« previous php.notes (#215175) next »