TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: HERMAN SCHONFELD
from: CHRIS DOWNS
date: 1997-03-31 12:09:00
subject: Re: Video

 NJ>  //Screen[x+y*320] = color;
 NJ>  Screen[x+(y<<6)+(y<<8)] = color; /* same as above but here the
 NJ>          multiplication is replaced with
 NJ>          shifts to make it faster */
 
 DM>Are you SURE that two shift operations followed by an addition is really
 DM>faster than the hardware-optimized integer multiplication?
 
 DM>Perhaps on an 8086... but on an 80486?  Probably not...
 HS> Bit shifting will ALWAYS be faster than multiplying.
Go back and re-read Darin's comment.  Yes, ONE bit shift is faster
than ONE multiply.  But again, look at the code.  It replaced
ONE add and ONE multiply with TWO adds and TWO bit shifts.  Is it
faster to do one multiply or two bit shifts and an add????
Well, this isn't a new discussion.  I did some simple testing some
time back (on a 386) and found that a bit shift and an add is
substantially slower (some 30%) than using an integer multiply.
Adding yet one more bit shift will slow it down even more.
DON'T USE BIT SHIFT/ADD TO REPLACE A MULTIPLY!!  You'll get fatter
code, slower code, and more obscured code.
And even in those cases where you are just replacing a multiply by
a constant power of two with a single bit shift (the ONLY case where
using the substitution is something other than a lose-lose situation),
choose the multiply if multiply "makes sense".  Even the most brain
dead of compilers will make this optimization for you.
---
 þ Blue Wave/QWK v2.12 þ
---------------
* Origin: St. Louis Users Group (1:100/4)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.