TIP: Click on subject to list as thread! ANSI
echo: z3_pascal
to: Frank Malcolm
from: Bob Lawrence
date: 1996-05-09 08:27:12
subject: StringToInteger

CG>  BL> function StrInt(s: string): integer;
 CG>  BL> var
 CG>  BL> i, size: integer;
 CG>  BL> begin
 CG>  BL>    i := 1; size := 0;
 CG>  BL>    while s[i] in ['0'..'9'] do begin
 CG>  BL>       size := size * 10 + (Ord(s[i]) - 48);
 CG>  BL>       inc(i);
 CG>  BL>    end;
 CG>  BL>    StrInt := size;
 CG>  BL> end;

 CG> You might save a smidgen of time passing a pointer into the
 CG> function rather than the string itself

 FM> Colin is right here, it would save time and it would be a
 FM> smidgin. :-)

  Actually, it nearly halved it. I was amazed.

 FM> Instead of a pointer, you could just declare the parameter
 FM> const.

  Eh? What's the point of converting a constant to an integer? I'd
already know the answer.

 CG> I think a set might use a fixed number of bytes regardless of
 CG> the number of elements you use. perhaps you should look into
 CG> other methods of testing each value...
 FM> He's right here, too - but it's probably worse than that. The
 FM> constant set is probably stored in the minimum number of bytes,
 FM> but then expanded to the maximum 32 bytes before being passed
 FM> to the set membership routines - which itself is a function
 FM> call. Use...

 while (s[i] > = '0') and (s[i] <= '9') do

  That's how I started, and it's slower than the set. In fact, a set
was what I tried last... and it's the fastest of all. I declared a
constant set and it's slower! God only knows why. Borland must do
something fiendishly clever with their sets. I love 'em.

 FM> This by itself will give you an amazing speed improvement, if
 FM> the code in BP7/Delphi which you're using is anything like that
 FM> which was generated in TP6, the last one I looked at in detail.

  Delphi has a StrToInt function. I'm in BP7.

 CG> You might also need something in there to tell it when it's
 CG> reached the end of the string.

 FM> And he's right here, too - add...

 FM> and (i <= length (s))

  I've structured my string with the numbers upfront to reduce the
loop, and if it doesn't find any numbers it stops anyway. If I were
writing a proper StrToInt I'd have to stop the integer overflowing
too.

 FM> ... to that while statement - you *might* get a string with all
 FM> numbers. Oh, and for safety you should be checking that you
 FM> don't overflow 32767. 

  See?

 FM> Finally, don't declare Size, and use Result := Result + etc,
 FM> Result := 0 before that and omit the final assignment.

  Result? BP7 doesn't have one. This isn't Delphi, you know... this is
the agricultural side of Pascal.

 FM> A late-breaking thought - remove the overflow test and put it
 FM> in an exception block.

  ROFL! If I were using Delphi, I'd use StrToInt()! I keep telling you
Pascal sucks but you don't listen!

 FM> Another one - have you already stripped whitespace *before* the
 FM> number? If not you might want to put another for loop before
 FM> that one, with a Break if s[i] isn't space or tab (and maybe
 FM> even Cr & LF).

  If I wanted something that slow, I could use an abacus.

 FM> And if speed is *that* important, do it in BASM. :-)

  Yair... I'll get around to that eventually, as soon as I can write a
program that doesn't FORMAT C: every time I use pointers.

Regards,
Bob


___ Blue Wave/QWK v2.12
@EOT:

---
* Origin: Precision Nonsense, Sydney (3:711/934.12)
SEEN-BY: 633/267 270
@PATH: 711/934 809 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™.