Subj : Re: faster approach to division To : comp.programming From : Walter Date : Mon Jul 11 2005 01:06 pm "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?) The behavior of CPUs on various operations is often not intuitive, so you should be prepared to test. If you're not seeing a worthwhile difference in your tests, then it's almost surely not an optimization worth spending time on. For some techniques for timing code, check out www.digitalmars.com/techtips/timing_code.html -Walter www.digitalmars.com C, C++, D compilers "code of the nerds" .