When creating a WinTask script, I often want to put my data items in a text file external to my script. That way, I don't have to modify the script to use a different set of test data.
Initially, I would read each data item from the file as I needed it
But, with larger sets of data items, this seemed slow.
So now, I read the entire set of data items into an array, then use the array elements as I need them. This is much faster.
Another benefit is that I can now use the array of items in any order I choose. Often I want to randomly pick one of the items and use it.
'
' Check performance difference between two methods of using data in a txt file
'
' Author: Joe Strazzere
'
Dim Items$(10000) ' An array to hold the data items
file$="C: est.txt" ' The file containing the data items
'
' This method simply reads an item from the data file when it is needed
'
Comment("Method 1 - Using each file item in turn")
StartTimer(1)
i=0
Repeat
Read(file$,Buffer$,CRLF)
Var$=(Buffer$)
' Comment(Var$)
i=i+1
Until eof(file$)=1
StopTimer(1)
t$=Str$(Timer(1))
i$=Str$(i)
MsgBox("This method took "+t$+"/100 seconds to process "+i$+" items")
'
' This methods first reads all the data items from the file into an array
' Then, as each data item is needed, the corresponding array element is used
'
' Not only is this method faster, but it has the added advantage of being
' able to use the items in any order desired - even randomly
'
Comment("Method 2 - Using an array and SplitIntoArray")
SetReadPos(file$,0)
StartTimer(2)
Buffer$=""
Repeat
Read(file$,TempBuffer$)
Buffer$=Buffer$+TempBuffer$
Until eof(file$)=1
ItemCount=SplitIntoArray(Buffer$,Items$(),CRLF)
i=0
Repeat
Var$=(Items$(i))
' Comment(Var$)
i=i+1
Until i > ItemCount
StopTimer(2)
t$=Str$(Timer(2))
i$=Str$(i-1)
MsgBox("This method took "+t$+"/100 seconds to process "+i$+" items")