On Apr 26, 2007, at 11:14 PM, Lucian Adrian Grijincu wrote:

in apr-conv10-faster.patch you added:

static const char digits[] = "0123456789";
*--p = digits[magnitude % 10];

Why is this faster than:
*--p = (char) '0' + (magnitude % 10); ?

For your "faster" version, under the hood, the C compiler adds
(magnitude % 10) to the address of digits and then copies the contents
of the memory location represented by the sum's result into *--p.

My version just adds (magnitude % 10) to '0' and stores the result in *--p.


Basically, the "faster" version would not be, assuming the cast
isn't that expensive...

This was run on an old FreeBSD box, and, ymmv, the ratios have proven
to be pretty constant on other systems and implementations (for
those of us old-timers, the biggest "surprise" may be that floating
point is no longer costly):

% ./cputimes
  Operation                         Clicks for each trial    Mics/N
Null Loop (n=10000000)
{} 7 8 6 8 9 0.01
Int Operations (n=10000000)
i1++ 7 8 8 8 7 0.00 i1 = i2 + i3 8 8 8 8 9 0.00 i1 = i2 - i3 9 10 10 9 8 0.00 i1 = i2 * i3 8 10 9 8 9 0.00 i1 = i2 / i3 46 46 47 46 46 0.03 i1 = i2 % i3 47 46 46 47 46 0.03
Float Operations (n=10000000)
f1 = f2 8 7 8 8 8 0.00 f1 = f2 + f3 8 8 9 7 9 0.00 f1 = f2 - f3 9 8 8 9 8 0.00 f1 = f2 * f3 8 7 8 8 8 0.00 f1 = f2 / f3 40 41 40 40 40 0.03
Numeric Conversions (n=10000000)
i1 = f1 69 70 69 70 69 0.05 f1 = i1 11 11 11 11 11 0.00
Integer Vector Operations (n=10000000)
v[i] = i 21 21 23 21 22 0.01 v[v[i]] = i 46 47 47 47 47 0.03 v[v[v[i]]] = i 48 47 48 47 47 0.03
Control Structures (n=10000000)
if (i == 5) i1++ 10 11 8 9 10 0.00 if (i != 5) i1++ 10 9 8 9 10 0.00 while (i < 0) i1++ 9 9 8 8 11 0.00 i1 = sum1(i2) 17 18 17 18 18 0.01 i1 = sum2(i2, i3) 19 20 19 20 19 0.01 i1 = sum3(i2, i3, i4) 24 23 24 23 24 0.01
Input/Output (n=1000000)
fputs(s, fp) 34 34 34 34 35 0.26 fgets(s, 9, fp) 27 27 27 27 27 0.20 fprintf(fp, sdn, i) 110 108 109 109 109 0.85 fscanf(fp, sd, &i1) 159 158 159 163 158 1.24
Malloc (n=1000000)
free(malloc(8)) 75 74 75 75 74 0.58 push(i) 51 51 53 54 53 0.40 i1 = pop() 27 28 28 28 28 0.21
String Functions (n=10000000)
strcpy(s, s0123456789) 52 52 53 52 53 0.04 i1 = strcmp(s, s) 7 9 6 9 9 0.00 i1 = strcmp(s, sa123456789) 26 25 23 23 24 0.01
String/Number Conversions (n=100000)
i1 = atoi(s12345) 4 3 4 4 3 0.28 sscanf(s12345, sd, &i1) 20 19 19 20 19 1.51 sprintf(s, sd, i) 9 9 10 9 9 0.71 f1 = atof(s123_45) 3 3 3 3 3 0.23 sscanf(s123_45, sf, &f1) 20 19 19 20 19 1.51 sprintf(s, sf62, 123.45) 29 29 30 30 29 2.29
Math Functions (n=1000000)
i1 = rand() 9 9 9 10 9 0.07 f1 = log(f2) 18 17 18 18 17 0.13 f1 = exp(f2) 30 29 29 30 29 0.22 f1 = sin(f2) 18 17 18 17 18 0.13 f1 = sqrt(f2) 12 11 12 12 11 0.08
Total Seconds:     48.34


Reply via email to