Efficient coding considered harmful?

T. William Wells bill at twwells.uucp
Fri Nov 4 02:30:35 AEST 1988


In article <8819 at smoke.BRL.MIL> gwyn at brl.arpa (Doug Gwyn (VLD/VMB) <gwyn>) writes:
: In article <137 at twwells.uucp> bill at twwells.UUCP (T. William Wells) writes:
: >#define HASHSIZE 4              /* A power of two, or else! */
:
: To continue my complaint against using bitwise operators where arithmetic
: is called for, let me point out that most reasonable hashing schemes work
: best of the hash table size is NOT a power of two.

But there are also some that don't.

:                                                     Now, if you had used
: arithmetic instead of bit-diddling throughout your hashing code, most
: likely a programmer could change HASHSIZE to some useful number like 113
: and that would be all it would take to improve the hashing scheme.

But on quite a few of the hashing schemes that use a power of two,
you *can't* change the number to anything other than a power of two.
It breaks the algorithm.

Actually, I almost agree with you here: if the hash method were not
one where the size had to be a power of two, it would be incorrect to
use bitwise operators. However, the mistake would have been in making
the requirement that a tunable parameter which is not naturally
restricted to a power of two be restricted to be a power of two.  That
mistake would then be compounded by taking advantage of the bogus
restriction. (Ack, I *hate* subjunctives!)

Formulated as a general principle: if an operand in a multiply or the
divisor in a divide or mod (with the other operand being known to be a
positive number) is one which in the problem being solved is
naturally a power of two, it is reasonable to use bitwise operators to
implement the operation, provided that a #define is used to define
the number and (when needed) its log base 2.

---
Bill
{uunet|novavax}!proxftl!twwells!bill



More information about the Comp.lang.c mailing list