towards a faster isdigit()
Paul Eggert
eggert at twinsun.com
Thu May 9 09:41:37 AEST 1991
steve at 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 <ctype.h>
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 <stdio.h>
#if traditional
# include <ctype.h>
#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;
}
More information about the Comp.lang.c
mailing list