The purpose was just to add many strings to another. When it's "directly" no problem but when the strings to add are passed as parameter of a procedure the execution time is very different.
Run the example.
Part 1/part2 directly and very fast
Part3 simulates my old code, very long time
Part4, using "mid(main_string,start)=add_string", the solution with time factor of #100.
In part1 and part2 there are few space allocations and main string moves.
In part3 there are allocations and moves for every adding
In part4 only one allocation and no main string move
Note : The final size not known so it's necessary to check when adding and increase the initial size if needed (not coded in that example).
Code: Select all
type ttest
strg as string
end type
dim shared as ttest test
dim shared as string addstrg,sg
dim shared as long start
start=1
addstrg="kdjfzifi zefoi zoefh zofhe ozhefo hefoh zoefh ohef hefo hefh he"
dim as integer ptr pstrg=@test.strg
dim as double tim
sub adding(sg as string)
test.strg+=sg
end sub
sub adding2(sg as string)
mid(test.strg,start)=sg
start+=len(sg)
end sub
print "string adress"," lenght"," space allocated"
''1/
test.strg=""
tim=timer
for i as long =1 to 10000
test.strg+=str(i)+addstrg+space(50)
if i>9990 then print *(pstrg),*(pstrg+1),*(pstrg+2)
next
print "time=";timer-tim," size=";len(test.strg)
print
''2/
test.strg=""
tim=timer
for i as long =1 to 10000
sg=str(i)+addstrg+space(50)
test.strg+=sg
if i>9990 then print *(pstrg),*(pstrg+1),*(pstrg+2)
next
print "time=";timer-tim," size=";len(test.strg)
print
''3/
test.strg=""
tim=timer
for i as long =1 to 10000
adding(str(i)+addstrg+space(50))
if i>9990 then print *(pstrg),*(pstrg+1),*(pstrg+2)
next
print "time=";timer-tim," size=";len(test.strg)
print
''4/
test.strg=space(2000000)
tim=timer
for i as long =1 to 10000
adding2(str(i)+addstrg+space(50))
if i>9990 then print *(pstrg),*(pstrg+1),*(pstrg+2)
next
print "time=";timer-tim,"real string size=";start-1
sleep