Subj : Re: faster approach to division To : comp.programming From : Rob Thorpe Date : Fri Jul 15 2005 02:23 am William wrote: > "Mark P" wrote in message > news:9JyAe.351$Rv7.249@newssvr21.news.prodigy.com... > > I'm working on a problem which involves a fair amount of integer > > division but in essentially all cases the divisor is +/- 1 or +/- 2. > > I'm using C++ and trying to figure out whether I should code this as > > just ordinary division or whether it makes sense to create a special > > class (basically a wrapped enumeration) to hold my possible divisor > > values. Then I would overload operator/() to recast division as a null > > operation or a sign flip and/or right shift. > > > > Anyone have any intuition on this? I've tried a few very simple tests > > comparing division and shifting and it's hard to discern a difference, > > but I'm not sure my tests are very effective. (Are modern machines > > optimized to divide by 1 and 2 quickly?) > > I assume you've got your compiler set to optimize for speed, > so what kind of assembly is it actually outputting? I'd > start there - you might find it's already made some good > choices. (For example, it probably has turned the integer > division into a right shift or something faster already. If > it didn't, it's either broken or your code didn't make that > replacement seem reasonable to it.) -Wm Or the language didn't make it reasonable to the compiler, as happens in some cases. Many languages round towards zero, right shifts round towards negative infinity for signed numbers. .