Re: Re: negative numbers

From: Date: Mon, 27 Dec 2004 10:16:21 +0000
Subject: Re: Re: negative numbers
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-205201@lists.php.net to get a copy of this message
On Monday 27 December 2004 12:40, Richard Lynch wrote: > If you want to mimic the behaviour of abs (allowing for positive numbers) > and performance was an issue, that: > $x = ($x < 0) ? - $x : $x; > > is most likely faster than abs() Having nothing better to do I decided to benchmark this: ternary: $doo = -20; for ($i = 1; $i < 10000000; $i++) { $dah = ($doo < 0) ? - $doo : $doo; } abs(): $doo = -20; for ($i = 1; $i < 10000000; $i++) { $dah = abs($doo); } It turns out that abs() is slightly faster - as you might have guessed, otherwise I wouldn't be posting this ;-) ternary = 14.67 secs abs() = 14.10 secs The moral of the story is: if speed is important to you always do your *own* benchmarking. -- Jason Wong -> Gremlins Associates -> www.gremlins.biz Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * ------------------------------------------ Search the list archives before you post http://marc.theaimsgroup.com/?l=php-general ------------------------------------------ /* A lot of people I know believe in positive thinking, and so do I. I believe everything positively stinks. -- Lew Col */

« previous php.general (#205201) next »