Dispatching a string

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
Lost Zergling
Posts: 538
Joined: Dec 02, 2011 22:51
Location: France

Dispatching a string

Post by Lost Zergling »

Hi everybody,
I have a code wich is parsing as string I want to parse in substring as fast as possible :

Code: Select all

For i=1 to Len(str_Tag) step istep
    Str_tmp=Mid(str_Tag,i, istep)
    ..
Next i
I tried a zstring Ptr code :

Code: Select all

Dim hlen as ubyte=3
Dim str_Tag as string= "abcdefghijklm"
Dim As ZString Ptr zp1
Dim As ZString Ptr zp2
Dim As ZString Ptr zp3=Allocate(1)
Dim As ZString Ptr Listptemp=Allocate(Len(str_Tag)*2+2)
Dim i as integer
Dim k as integer

For k=1 To 1 '00000  ' speed test

    *Listptemp=str_Tag
    zp1=Listptemp
    'For i=0 to Fix(Len(str_Tag)/hlen)  step hlen
    For i=1 to Len(str_Tag)  step hlen
        zp2=zp1
        zp1+=hlen 
        *zp3=*zp1    
        *zp1=0
        Print  *zp2 
        *zp1=*zp3
    Next i

Next k

Deallocate(Listptemp)
'Deallocate(zp3)
Print "Fin OK"
sleep : system
seems to be stable, but, ...looks likes pretty exactly same speed the code with Mid !!
Does anyone having any idea may going faster ?

ps : small issue deallocating zp3 (0.23/W7 32 bits)
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Dispatching a string

Post by MrSwiss »

FYI -- Tips & Tricks states:
Source-code only - please, don't post questions here.
(seen on Mouse hover, over Title)

For questions use: (in Programming)
Beginners
General (more involved problems)
or (on platforms)
DOS / Linux / Windows (for OS depending stuff)
Lost Zergling
Posts: 538
Joined: Dec 02, 2011 22:51
Location: France

Re: Dispatching a string

Post by Lost Zergling »

Unexpected. A little bit puritan or what ?.. Nevertheless your remark sounds thrue indeed. Well, ok then.
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Dispatching a string

Post by MrSwiss »

Lost Zergling wrote:little bit puritan
Not puritan, it's for solutions, instead of problems.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Dispatching a string

Post by fxm »

I only corrected your original code that crashes.
You must work at the ubyte assignment level and not at zstring assignment level:

Code: Select all

Dim hlen as ubyte=3
Dim str_Tag as string= "abcdefghijklm"
Dim As ZString Ptr zp1
Dim As ZString Ptr zp2
Dim As ZString Ptr zp3=Allocate(1)
Dim As ZString Ptr Listptemp=Allocate(Len(str_Tag)*2+2)
Dim i as integer
Dim k as integer

For k=1 To 1 '00000  ' speed test

    *Listptemp=str_Tag
    zp1=Listptemp
    'For i=0 to Fix(Len(str_Tag)/hlen)  step hlen
    For i=1 to Len(str_Tag)  step hlen
        zp2=zp1
        zp1+=hlen
        (*zp3)[0]=(*zp1)[0]   
        (*zp1)[0]=0
        Print  *zp2
        (*zp1)[0]=(*zp3)[0]
    Next i

Next k

Deallocate(Listptemp)
Deallocate(zp3)
Print "Fin OK"
sleep : system
(*zp3)[0] could be replaced by a simple ubyte

Admin, could you move this thread to the beginners forum ?
Lost Zergling
Posts: 538
Joined: Dec 02, 2011 22:51
Location: France

Re: Dispatching a string

Post by Lost Zergling »

Nice tips. Thanks a lot. Tested with an uByte ok. I will test speed tomorrow. If significant, even a very small improvment , if efficient on every piece of key (out from millions) can make a significant marginal speed difference at the end. Maybe worthy.
ps : move this thread where you want, no problem.
Result of testing : typical speed gain +0% to +20% (list of 1Million keys*6 renew) : the bigger the volume and the mass of operations, the more noticeable the gain in speed.
Post Reply