Both line input and redim preserve are slow.
Test 3 methods
1)two line input loops with no redim 3 seconds
2) one line input with redim --jj2007 6 seconds
3) load to a string then split the string. < 1 second
Both my methods are 1 based arrays, jj2007 uses 0 based arrays.
Code: Select all
#include "file.bi"
'========================= method 1 =================
Function Load(file As String,a() as string) As long
If Fileexists(file)=0 Then Print file;" not found":Sleep:End
Dim As Long f=Freefile,counter
Open file For Binary Access Read As #f
dim as string tmp
while not eof(f)
counter+=1
line input #f,tmp
wend
redim a(1 to counter):counter=0
seek #f,1
while not eof(f)
counter+=1
line input #f, a(counter)
wend
Close #f
Return counter
End Function
'=========================== method 2 =================
Function Recall(fname As String, recstr() As String) As Integer
Dim As Integer ct=0
If Open(fname For Binary Access Read As #1) = 0 Then
While Not Eof(1)
ReDim Preserve recstr(ct)
Line Input #1, recstr(ct)
ct=ct+1
Wend
Close #1
Else
Print "Error opening file"
End If
Return ct
End Function
'=============================method 3===================
Function loadfile(file As String) As String
If Fileexists(file)=0 Then Print file;" not found":Sleep:End
Dim As Long f=Freefile
Open file For Binary Access Read As #f
Dim As String text
If Lof(1) > 0 Then
text = String(Lof(f), 0)
Get #f, , text
End If
Close #f
Return text
End Function
Function StringSplit(s_in As String,chars As String,result() As String) As Long
Dim As Long ctr,ctr2,k,n,LC=Len(chars)
Dim As boolean tally(Len(s_in))
#macro check_instring()
n=0
While n<Lc
If chars[n]=s_in[k] Then
tally(k)=true
If (ctr2-1) Then ctr+=1
ctr2=0
Exit While
End If
n+=1
Wend
#endmacro
#macro split()
If tally(k) Then
If (ctr2-1) Then ctr+=1:result(ctr)=Mid(s_in,k+2-ctr2,ctr2-1)
ctr2=0
End If
#endmacro
'================== LOOP TWICE =======================
For k =0 To Len(s_in)-1
ctr2+=1:check_instring()
Next k
If ctr Then Redim result(1 To ctr): ctr=0:ctr2=0 Else Return 0
For k =0 To Len(s_in)-1
ctr2+=1:split()
Next k
'===================== Last one ========================
If ctr2>0 Then
Redim Preserve result(1 To ctr+1)
result(ctr+1)=Mid(s_in,k+1-ctr2,ctr2)
End If
Return Ubound(result)
End Function
'create a text file
sub savefile(filename as string,text as string)
var h=freefile
open filename for binary access write as #h
put #h,1,text
close #h
end sub
sub makeafile
dim as string s
for n as long=1 to 500000
s+=str(n)+chr(13,10) 'a common line ending
next
savefile("test.txt",s)
end sub
makeafile
dim as double t
t=timer
redim as string a()
load("test.txt",a())
print "Time ";timer-t; " Method 1 line input two loops"
for n as long=lbound(a) to 20
print n,a(n)
next
t=timer
redim as string b()
recall("test.txt",b())
print "Time ";timer-t;" Method 2 line input redim preserve"
for n as long=lbound(b) to 20
print n,b(n)
next
t=timer
redim as string c()
var txt=loadfile("test.txt")
stringsplit(txt,chr(13,10),c())
print "Time ";timer-t;" Method 3 split text"
for n as long=lbound(c) to 20
print n,c(n)
next
print "done"
sleep
kill "test.txt"