BIT Counting: C programming problem

Barry Margolin barmar at think.COM
Wed Jan 11 19:42:19 AEST 1989


I haven't seen anyone so far suggest the far end of the time/space
trade-off: have a 64K table, with each element N containing the number
of bits in N (you'll probably have to use one of the slower mechanisms
to compute the contents of this table, but it only needs to be run
once when writing the program and then incorporated into the program
text as an initial value).  Someone else came close by suggesting a
256-element table which would be consulted twice and then the results
added.  But is an extra 64Kbytes of data much of a problem in these
days of large and/or virtual memories?

One argument against this in a paging environment is that this is more
likely to cause a page fault than other mechanisms that use less data
and more computation, and a page fault is at least an order of
magnitude more expensive than arithmetic instructions.  And in
swapping systems it will increase process loading time.  But on 1Mb PC
it's only 6% of your memory capacity, and it might be worth it
depending on the application.

Barry Margolin
Thinking Machines Corp.

barmar at think.com
{uunet,harvard}!think!barmar



More information about the Comp.lang.c mailing list