Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!ucla-cs!twinsun!eggert From: eggert@twinsun.com (Paul Eggert) Newsgroups: comp.lang.c Subject: Re: towards a faster isdigit() Message-ID: <1991May8.234137.11841@twinsun.com> Date: 8 May 91 23:41:37 GMT References: <1991May8.030515.7004@twinsun.com> <716@taumet.com> Sender: usenet@twinsun.com Organization: Twin Sun, Inc Lines: 45 Nntp-Posting-Host: ata steve@taumet.com (Stephen Clamage) writes: >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. That's odd. I tried several systems and got speedups for the proposed isdigit() every time, if anything even more for RISC than CISC. E.g.: prop trad speedup 2.5 4.5 1.8 Sparcstation 1 SunOS 4.1.1 cc -O 3.6 5.4 1.5 DECstation 3100 Ultrix V4.0 (Rev. 179) cc -O 9.7 13.3 1.4 Sun-3/260 SunOS 4.1 cc -O prop = time with proposed ``#define isdigit(c) ((unsigned)((c)-'0') < 10))'' trad = time with traditional isdigit() in speedup = trad/prop = how much faster the proposed isdigit() is All times are the sum of the user+system CPU time in seconds, and are measured with the command `time ./a.out 10000000' under csh. Here's the program I used. #include #if traditional # include #else # define isdigit(c) ((unsigned)((c) - '0') < 10) #endif main(argc, argv) int argc; char **argv; { char *p = argv[1]; int i = atoi(p), c = *p; while (0 <= --i) if (isdigit(c)) c = ++*p; printf("%s\n", p); return 0; }