EmbeddedRelated.com
Forums
The 2026 Embedded Online Conference

Slightly OT: speed of operation on a PC

Started by Tim Wescott August 4, 2015
Which do you think is quicker on a PC, with the latest gnu compiler:

double a = something;
double b = something else;

if (a >= 0.0 && b < 0.0)

>> or <<
if (a * b <= 0) Thank you for your time. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
On 8/4/2015 1:59 PM, Tim Wescott wrote:
> Which do you think is quicker on a PC, with the latest gnu compiler: > > double a = something; > double b = something else; > > if (a >= 0.0 && b < 0.0) > >>> or << > > if (a * b <= 0) > > Thank you for your time.
My guess would be the latter if any. Why not write the code and look at what is generated? -- Rick
Le 04/08/2015 19:59, Tim Wescott a &eacute;crit :
> Which do you think is quicker on a PC, with the latest gnu compiler: > > double a = something; > double b = something else; > > if (a >= 0.0 && b < 0.0) > >>> or << > > if (a * b <= 0) > > Thank you for your time. >
if (a + b < a) would be quicker, I believe.
On 8/4/2015 10:59 AM, Tim Wescott wrote:
> Which do you think is quicker on a PC, with the latest gnu compiler: > > double a = something; > double b = something else; > > if (a >= 0.0 && b < 0.0) > >>> or << > > if (a * b <= 0) > > Thank you for your time.
Do you really need to micro-optimize like this? Why not just have the code *say* what you are trying to *do*?
Tim Wescott <seemywebsite@myfooter.really> writes:
> Which do you think is quicker on a PC, with the latest gnu compiler: > if (a >= 0.0 && b < 0.0) >>> or << > if (a * b <= 0)
It depends partly on the hardware. You have to actually benchmark on your target cpu.
"Tim Wescott" <seemywebsite@myfooter.really> wrote in message 
news:juKdnf5uzZr0YF3InZ2dnUU7-YOdnZ2d@giganews.com...
> Which do you think is quicker on a PC, with the latest gnu compiler: > > double a = something; > double b = something else; > > if (a >= 0.0 && b < 0.0) > >>> or << > > if (a * b <= 0) > > Thank you for your time. > > -- > > Tim Wescott > Wescott Design Services > http://www.wescottdesign.com
if a==1 and b==0, first expression is false, second expression is true so they are not equivalent. Border conditions are such a bother. I could proivide a similar expresion which is even faster but laso not right (0, for example). Sometimes the use of the sgn() function is even quicker. Rules of optimsiation: 1) Never 2) Later 3) Measure
On Tue, 04 Aug 2015 20:06:35 +0200, Lanarcam wrote:

> Le 04/08/2015 19:59, Tim Wescott a &eacute;crit : >> Which do you think is quicker on a PC, with the latest gnu compiler: >> >> double a = something; >> double b = something else; >> >> if (a >= 0.0 && b < 0.0) >> >>>> or << >> >> if (a * b <= 0) >> >> Thank you for your time. >> > if (a + b < a) would be quicker, I believe.
I don't think that tests if a and b are of opposite sign. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
On Tue, 04 Aug 2015 11:07:49 -0700, Don Y wrote:

> On 8/4/2015 10:59 AM, Tim Wescott wrote: >> Which do you think is quicker on a PC, with the latest gnu compiler: >> >> double a = something; >> double b = something else; >> >> if (a >= 0.0 && b < 0.0) >> >>>> or << >> >> if (a * b <= 0) >> >> Thank you for your time. > > Do you really need to micro-optimize like this?
Uh -- fun?
> Why not just have the code *say* what you are trying to *do*?
Where's the job security in that? -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
Le 04/08/2015 20:27, Tim Wescott a &eacute;crit :
> On Tue, 04 Aug 2015 20:06:35 +0200, Lanarcam wrote: > >> Le 04/08/2015 19:59, Tim Wescott a &eacute;crit : >>> Which do you think is quicker on a PC, with the latest gnu compiler: >>> >>> double a = something; >>> double b = something else; >>> >>> if (a >= 0.0 && b < 0.0) >>> >>>>> or << >>> >>> if (a * b <= 0) >>> >>> Thank you for your time. >>> >> if (a + b < a) would be quicker, I believe. > > I don't think that tests if a and b are of opposite sign. >
That's right, my error. You could try the macro signbit if (signbit(a) != signbit(b))
On 8/4/2015 11:28 AM, Tim Wescott wrote:
> On Tue, 04 Aug 2015 11:07:49 -0700, Don Y wrote: > >> On 8/4/2015 10:59 AM, Tim Wescott wrote: >>> Which do you think is quicker on a PC, with the latest gnu compiler: >>> >>> double a = something; >>> double b = something else; >>> >>> if (a >= 0.0 && b < 0.0) >>> >>>>> or << >>> >>> if (a * b <= 0) >>> >>> Thank you for your time. >> >> Do you really need to micro-optimize like this? > > Uh -- fun?
If you want to head down that path, there are lots of processor/compiler specific "optimizations" that you can explore! The problem then becomes making sure you don't let some "clever trick" creep into your code at a later date. Given a year expressed in packed BCD, indicate whether the year is a leap year. Obviously, there is some merit in knowing this. OTOH, the code only has to execute VERY rarely! [It's an interesting problem, though. How many clock cycles will it take to resolve? :> ]
>> Why not just have the code *say* what you are trying to *do*? > > Where's the job security in that?
Do you *really* want to be tethered to "old projects"? Or, would you rather be free to explore *new* projects?? ;-)
The 2026 Embedded Online Conference