Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace <-@.> Newsgroups: comp.lang.java.programmer Subject: Re: Using Enumerated Types as Array Indexes Date: Thu, 18 Aug 2011 11:50:11 -0700 Organization: A noiseless patient Spider Lines: 32 Message-ID: References: <4e4b0d81$0$314$14726298@news.sunsite.dk> <4e4b1fe4$0$314$14726298@news.sunsite.dk> <6d418cda-dab3-43df-a9ec-293b43f2bbd8@glegroupsg2000goo.googlegroups.com> <9b2aglFuj7U1@mid.individual.net> <__-dnaxcSIRV-NHTnZ2dnUVZ_tOdnZ2d@earthlink.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 18 Aug 2011 18:50:22 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="XjIWM99mD7Ijfdu600oVPA"; logging-data="31768"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19851NrnDRmsOFy3vsc75rTd1wyX1e0TW0=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0) Gecko/20110812 Thunderbird/6.0 In-Reply-To: Cancel-Lock: sha1:sRhlx026YCk8+ayrlXYPHMnqC04= Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:7225 On 8/18/2011 11:37 AM, Andreas Leitgeb wrote: > Apart from that, it is a dirty hack to avoid conditionals. One thing to keep in mind here I think is the relative execution time for each operation: 1. % and / are division. Some CPUs execute this as quickly addition and subtraction, some require a large number of CPU cycles. 2. Conditionals tend to be a subtraction, followed by a branch. For a simple branch like this: (n < 0) ? -n : n Might be implemented like this CMPZ n ;; these are not byte codes, just psudeo-x86 JLT +2 NEG n ... ;; result of JLT In other words, there's little opportunity for pipeline stall here. The instruction after NEG n is executed regardless, and should be in the pipe no matter what. I'm pretty sure that most CPUs will deal with executing one instruction like NEG conditional easily (most will compute the result and then just not use it if the conditional JLT is taken). So preferring modulus to ?: or Math.abs() might be a misoptimization.