Path: csiph.com!x330-a1.tempe.blueboxinc.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Tue, 26 Apr 2011 15:09:40 -0500 Date: Tue, 26 Apr 2011 13:09:34 -0700 From: Patricia Shanahan User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.15) Gecko/20110303 Thunderbird/3.1.9 MIME-Version: 1.0 Newsgroups: comp.lang.java.programmer Subject: Re: Java left shift and right shift operators. References: <295e16b3-2ed8-4529-bfb0-1cc26ed93ad6@d26g2000prn.googlegroups.com> <188cz6ta97kkm$.dlg@kimmeringer.de> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: Lines: 24 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 75.8.126.96 X-Trace: sv3-rDfA5HwbxNOdieaj/Rtvb3Tsk8v7vC8V2KNr7kNlaVY8jKmXFt/Ul/yXDVeS2OLenygxVj3oJiSsXcQ!NERNPYHTlqkrYDZPKAA0PhryI8WUmhM2HfoZcYBANzW0tOBGFyCPqWwhAj/ckb1RZGieCagVy6U7!AVVAddcR+9qcPNtIi8HCWORCiSJ6zat1kJKPb5Xvrio= X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2524 Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:3286 On 4/26/2011 11:09 AM, Patricia Shanahan wrote: > On 4/26/2011 9:32 AM, Sanny wrote: > ... >> I have computed and found "if statments" take 20-50 times longer than >> basic arithmetic operations. So I wanted to replace if condition by >> some arithmetics/ Maths. > ... > > You are seriously underestimating the range of variation of if-statement > time. It can be just as fast as an arithmetic statement. It must be > measured in context - you could be wasting your time if the actual mix > of left and right shifts has a pattern the branch predictor can pick up. Incidentally, one of the fastest cases arises during multi-word shifting. Each word in the shift will have the same pattern of taken and not-taken branches. If you do an N-word shift, about (N-1)/N branches will be correctly predicted by just about any reasonable algorithm, and go as fast as a simple arithmetic operation. The Math methods also need to be measured. The native implementations will use whatever fast hardware is available, but some operations may not have hardware support on every system. Patricia