Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!wuarchive!uunet!taumet!steve From: steve@taumet.com (Stephen Clamage) Newsgroups: comp.lang.c Subject: Re: towards a faster isdigit() Message-ID: <716@taumet.com> Date: 8 May 91 15:41:58 GMT References: <1991May8.030515.7004@twinsun.com> Organization: Taumetric Corporation, San Diego Lines: 34 eggert@twinsun.com (Paul Eggert) writes: >The traditional implementation of isdigit() in is typically >something like this: > #define isdigit(c) ((_ctype_+1)[c] & 4) >which requires indexing through a global array followed by a masking >operation. Why not use the following implementation instead? > #define isdigit(c) ((unsigned)((c)-'0') < 10) >This needs just a subtraction followed by a comparison. It's faster on >all the systems I've tried it on, and is strictly conforming ANSI C. Maybe you didn't try it on enough systems :-) The macro you suggest requires a test and jump, and on some modern RISC machines the penalty is very high. For example, I tried a loop which only tested for isdigit and incremented a global based on the test. On a Sun-4 (SPARC), the program using your macro took 44% longer than the one using the usual ctype macro. On a DECStation (MIPS), your macro took 46% longer. This points up the principle that you shouldn't diddle your code looking for small speedups based on characteristics of the compiler or target machine. Your attempts may backfire badly when the code is re-used. (I'm not accusing Paul of diddling, I just like to make this point.) Apart from that, the other ctype tests (except isascii) are always faster using the lookup table, and it is simpler to use a uniform set of macros. -- Steve Clamage, TauMetric Corp, steve@taumet.com