Board index » delphi » IntToStr/StrToInt

IntToStr/StrToInt


2005-08-12 07:55:15 AM
delphi152
Hi, I just recently came across the fastcode project online. I have kept
my own unit over the years with asm optimized alternatives to system
functions.
a couple that I use often are inttostr and strtoint. I didn't find these
included in the fastcode project. I am also not sure where to submit code
for this project so I figured I'd just post it here:
these are signed versions. I never had a need for unsigned versions so I
never made them...
function IntToStr(Value: Integer): ShortString;
// Value = eax
// Result = edx
asm
push ebx
push esi
push edi
mov edi,edx
xor ecx,ecx
mov ebx,10
xor edx,edx
cmp eax,0 // check for negative
setl dl
mov esi,edx
jnl @reads
neg eax
@reads:
mov edx,0 // edx = eax mod 10
div ebx // eax = eax div 10
add edx,48 // '0' = #48
push edx
inc ecx
cmp eax,0
jne @reads
dec esi
jnz @positive
push 45 // '-' = #45
inc ecx
@positive:
mov [edi],cl // set length byte
inc edi
@writes:
pop eax
mov [edi],al
inc edi
dec ecx
jnz @writes
pop edi
pop esi
pop ebx
end;
function StrToInt(Value: ShortString): Integer;
// Value = eax
// Result = eax
asm
push ebx
push esi
mov esi,eax
xor eax,eax
movzx ecx,Byte([esi]) // read length byte
cmp ecx,0
je @exit
movzx ebx,Byte([esi+1])
xor edx,edx // edx = 0
cmp ebx,45 // check for negative '-' = #45
jne @loop
dec edx // edx = -1
inc esi // skip '-'
dec ecx
@loop:
inc esi
movzx ebx,Byte([esi])
imul eax,10
sub ebx,48 // '0' = #48
add eax,ebx
dec ecx
jnz @loop
mov ecx,eax
and ecx,edx
shl ecx,1
sub eax,ecx
@exit:
pop esi
pop ebx
end;
 
 

Re:IntToStr/StrToInt

Hi gordy
I didn't measure any difference to the RTL versions. Delphi 2005, Win XP.
cheers
michael
"gordy" <XXXX@XXXXX.COM>writes
Quote
Hi, I just recently came across the fastcode project online. I have kept
my own unit over the years with asm optimized alternatives to system
functions.

a couple that I use often are inttostr and strtoint. I didn't find these
included in the fastcode project. I am also not sure where to submit code
for this project so I figured I'd just post it here:

these are signed versions. I never had a need for unsigned versions so I
never made them...
 

Re:IntToStr/StrToInt

Hi Gordy.
An IntToStr challenge does already exist
dennishomepage.gugs-cats.dk/IntToStrChallenge.htm
Although your IntToStr function is approx. twice as fast as the existing
D2005 version, it is not an equivalent of the RTL function since it returns
a ShortString instead of an AnsiString (the compiler would need to add code
to do the necessary conversions). It is also not as fast as some of the
existing FastCode versions.
regards
John
 

Re:IntToStr/StrToInt

John O'Harrow writes:
Quote
Hi Gordy.

An IntToStr challenge does already exist
dennishomepage.gugs-cats.dk/IntToStrChallenge.htm

Although your IntToStr function is approx. twice as fast as the existing
D2005 version, it is not an equivalent of the RTL function since it returns
a ShortString instead of an AnsiString (the compiler would need to add code
to do the necessary conversions). It is also not as fast as some of the
existing FastCode versions.

regards
John


Thanks.. the IntToStr functions are impressive. Is there no StrToInt
challenge tho?
 

Re:IntToStr/StrToInt

"gordy" <XXXX@XXXXX.COM>writes
Quote
John O'Harrow writes:
>Hi Gordy.
>
>An IntToStr challenge does already exist
>dennishomepage.gugs-cats.dk/IntToStrChallenge.htm
>
Thanks.. the IntToStr functions are impressive. Is there no StrToInt
challenge tho?
No StrToInt challenge yet.. Dennis, another one to add to the list?
regards,
John.
 

Re:IntToStr/StrToInt

Hi John
Quote
No StrToInt challenge yet.. Dennis, another one to add to the list?
Yes ;-)
Regards
Dennis
 

Re:IntToStr/StrToInt

Hi Dennis and John,
Quote
>No StrToInt challenge yet.. Dennis, another one to add to the list?
Yes ;-)
Is StrToInt a good candidate for a Fastcode challenge though? I don't think
anyone would want to use it in speed critical code, since I reckon it will
always be a slowish function.
IntToStr you often have to use to generate human readable text from binary
numbers, but I find that StrToInt is used much less.
Regards,
Pierre
 

Re:IntToStr/StrToInt

"Pierre le Riche" <XXXX@XXXXX.COM>writes
Quote

IntToStr you often have to use to generate human readable text from binary
numbers, but I find that StrToInt is used much less.

Not necessarily; it depends on your application. I write software for use
in laboratories - everything from sample login to final reports. I use both
IntToStr AND StrToInt extensively. There are lots of places where integer
values are entered either by a human, or imported via a text or xls file,
especially when we get to raw data import from the instruments. It may not
be so important when the data is input by humans, as the limiting factor
there is input time, however, when I am reading in a 12MB text file
containing thousands of integers, it would certainly help. I am probably in
the minority in how much data I import, but I still think it would be a
usefull general purpose function.
Dave White
SpectraChrom Software
www.spectrachrom.com
 

Re:IntToStr/StrToInt

The HyperStr libraries have USIToStr and StrToUSI assembly functions
that are very compact and very very fast.
Mike
 

Re:IntToStr/StrToInt

On Thu, 18 Aug 2005 23:25:21 +0200, "Pierre le Riche"
<XXXX@XXXXX.COM>writes:
Quote
Is StrToInt a good candidate for a Fastcode challenge though? I don't think
anyone would want to use it in speed critical code, since I reckon it will
always be a slowish function.
Well, almost every time you read integer data from a text file or
communications link you will need to convert it to integer.
XML/HTML parsing?
Reading .CSV files?
Or take the application that is my main responsibility at work, it is a
'protocol converter' sitting between a Coordinate Measuring Machine
(CMM) and the software that does actual measurement calculations and
sends movement commands to the CMM.
This program creates an abstraction layer around the CMM, so that the
main software (can be different programs from different vendors) doesn't
have to know very much about CMM peculiarities (can be any one of all
the different CMM:s we've produced in the last 15-20 years).
Even with a slow CMM, the amount of data sent back and forth is
respectable, adding speed and a 'scanning probe' (continous sending of
data samples) to the CMM increases the volume up to any amount.
I'm not saying that StrToInt is a bottleneck, but every little bit
helps...
--
Anders Isaksson, Sweden
BlockCAD: web.telia.com/~u16122508/proglego.htm
Gallery: web.telia.com/~u16122508/gallery/index.htm
 

Re:IntToStr/StrToInt

I also use StrToInt a lot. I agree with you that it is kind of slow, but
StrToFloat is much worse that this. VAL is much faster but it does not
supports regional settings, so optimized versions of StrToFloat and StrToInt
would be welcome.
Richard
"Pierre le Riche" <XXXX@XXXXX.COM>writes
Quote
Hi Dennis and John,

>>No StrToInt challenge yet.. Dennis, another one to add to the list?
>Yes ;-)

Is StrToInt a good candidate for a Fastcode challenge though? I don't
think anyone would want to use it in speed critical code, since I reckon
it will always be a slowish function.

IntToStr you often have to use to generate human readable text from binary
numbers, but I find that StrToInt is used much less.

Regards,
Pierre

 

Re:IntToStr/StrToInt

"Pierre le Riche" writes
Quote
Hi Dennis and John,


Is StrToInt a good candidate for a Fastcode challenge though? I don't
think
anyone would want to use it in speed critical code, since I reckon it will
always be a slowish function.

ISO2709 standard writes lengths of records, field postions and length as as
strings.