TIP: Click on subject to list as thread! ANSI
echo: z3_pascal
to: Colin Gunningham
from: Frank Malcolm
date: 1996-05-15 06:50:36
subject: StringToInteger

Hi, Colin.

CG>  BL> Passing the string in incrementing the string took 82uS. Passing the
CG>  BL> pointer and incrementing the pointer took 46uS. I'm there! Whoopee!

CG> From possibly faulty late nite memory, I think you were before doing:

CG> func (str:string)

CG> I meant you could do it thus:

CG> func (VAR str:string)

Or (const Str: string) if he was using Delphi - which he's not for this
one. But what he'd done is (Str: PString); type PString = ^string;

CG> I was thinking of maybe stuff like a case sequence
CG> for your '0' to '9's sort of thing, 'spose you've tried it :)

CG>  CG> Maybe storing the Ord() - 48 values in a lookup table could
CG>  CG> speed things up ?

CG>  BL> I don't know what you mean. Now I'll have to look up lookup tables
CG>  BL> (grin). I thought a set was a table.

CG> well I keep seeing that math calculation happening again
CG> and again: Ord(p^) - 48;

The code generated is simpler for the subtraction. The Ord() function
does nothing (no code generated), it just tells the compiler what you
want to do. The lookup table needs to calculate the memory address to
look up. Actually I think the timing would be close... OK, I'll check
it. :-) Here's the code...

PROGRAM.7: i := ord(c) - 48;
  cs:000F A04600         mov    al,[0046]
  cs:0012 30E4           xor    ah,ah
  cs:0014 2D3000         sub    ax,0030
  cs:0017 A34400         mov    [0044],ax

Hmmm, if I'd made i a byte the XOR wouldn't be necessary and the SUB
would be a byte operation.

PROGRAM.8: i := table[c];
  cs:001A A04600         mov    al,[0046]
  cs:001D 30E4           xor    ah,ah
  cs:001F 8BF8           mov    di,ax
  cs:0021 D1E7           shl    di,1
  cs:0023 8B85E8FF       mov    ax,[di-0018]
  cs:0027 A34400         mov    [0044],ax

Hmmm again, could drop the XOR similarly, and making the table an array
of byte would kill the SHL. Making the table a full 256 bytes (or at
least 57) would change the [di-0018] to just [di] too.

But the subtraction *is* faster.

CG> I forget how set's are stored, but a lookup table would be an
CG> array of all your values for Ord ('0'..'9') - 48, precalculated.
CG> worth checking out which combinations are faster

The set question doesn't arise in this part of the code although it does
in the earlier bit. Borland appear to have really smartened up their set
handling in BP7 (which Bob is using) from BP6 (which I use (or Delphi)).
I must check out what they've done sometime, because Bob's timing on
that bit shows that sets beat comparing the values. I'm surprised.

Regards, fIM.

 * * Raise public esteem, fire the liberal Congress
@EOT:

---
* Origin: Pedants Inc. (3:711/934.24)
SEEN-BY: 633/267 270
@PATH: 711/934 808 50/99 635/544 727 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.