trying to simplify an algorithm
zhouhui at wam.umd.edu
Mon Feb 28 07:49:58 PST 2005
On Mon, Feb 28, 2005 at 09:40:20AM +0000, Johan V. wrote:
>Hui Zhou wrote:
>> On Mon, Feb 28, 2005 at 01:36:32AM +0000, Johan V. wrote:
>>>return ( a+b+c == 180 ) * (a>0) * (b>0) * (c>0) *
>>> ( 1 + (a==b) + (a==c) + (b==c) );
>>>0 : not a triangle
>>>1 : scalene
>>>2 : isosceles
>>>4 : equilateral
>> Very Clever! Except ...
>> 1. Obscured
>> 2. Wasting CPU clocks
>I know it's very obscured. Think about the wasted clock cycles for a minute,
>the above can be implemented without conditional jumps ...
Obscured mostly because it reles on the fact that compiler treats
boolean as 1 or 0, not a fact supposed to be relied on.
Wasting CPU because it asks the cpu to gothrough all the comparisons
and additional multiplication and summation. If the compiler is smart
enough ot optimize away multiplication of 0 and summation of 0, it
goes back to the original conditional jumps' version.
(a || b || c) && ... is known to be shortcut,
will (a+b+c) * ... be shortcut?
Frankly I am not sure whether gcc will optimize away those
multiplications and summations. I will appreciate it if you will find
out and let me know.
More information about the lfs-chat