fast code and no morals
Doug Gwyn
gwyn at brl-smoke.ARPA
Sun Feb 16 15:26:33 AEST 1986
>Even if 'fast but no morals' doesn't optimize as well on the VAX as it does
>on the 68K, it still runs. The examples are common profiler hot spots.
>bcopy(dest,src,len)
>REG char *dest,*src;
>REG USHORT len;
>{
>while(len--)
> *dest++=*src++;
>}
>
>may look prettier than
>
>bcopy(dest,src,len)
>REG char *dest,*src;
>REG USHORT len;
>{
>if(len)
>do{
> *dest++=*src++;
> }while(--len)
>}
>
>but when you are calling bcopy to move 2k before the next
>interrupt hits, the extra 10us per iteration (dbgt vs. test,bra)
>can get real costly. Adding automatic backward detection (used in editors)
>would be difficult in assembler, but not in C.
Neither of these examples uses obscure code (except for your
redefining C keywords for no good reason). Also, neither
one is the best implementation of bcopy() on most machines.
One can do much better than byte transfers within a loop.
If you're going to worry about efficiency, do it right; use
the memcpy() library routine which some system programmer
has tuned for fastest possible block transfer, rather than
writing your own.
More information about the Comp.lang.c
mailing list