Faster file handling
Posted: Sat Aug 05, 2006 3:02 pm
Continued from:
http://www.purebasic.fr/english/viewtopic.php?t=23007
I re-wrote a program I had originally written in PowerBasic into PureBasic because I wanted to give it a GUI to make it easier to use. PowerBasic's DDT system is overly complex compared to PureBasic's Gadget system.
End result: The PowerBasic program finished the file in less time than it took the PureBasic program to do one percent of the file!
So I know file operations (specifically string reading and writing) can be a lot faster.
The file is in comma separated fields and all I'm doing is combining a couple of the fields into one field, formatting a couple of others, and deleting a few others. Then writing the result to an output file.
Here's the code:
I had ran into this before (as did other PureBasic users) with v3.x and switched to PowerBasic. The problem with PowerBasic is that it assumes you know and understand Windows API programming because that's all their DDT (Dynamic Dialog Tools) system is is just built in commands that mirror the API. When I discovered that PureBasic had released version 4 I decided to try it. It's nice but it appears the file reading and writting is still as slow as it was before.
BTW: I saw references to Rings' FastFile library but it doesn't appear to be on any of the PureBasic support sites like PureProject or PureArea. Is it no longer available?
http://www.purebasic.fr/english/viewtopic.php?t=23007
I re-wrote a program I had originally written in PowerBasic into PureBasic because I wanted to give it a GUI to make it easier to use. PowerBasic's DDT system is overly complex compared to PureBasic's Gadget system.
End result: The PowerBasic program finished the file in less time than it took the PureBasic program to do one percent of the file!
So I know file operations (specifically string reading and writing) can be a lot faster.
The file is in comma separated fields and all I'm doing is combining a couple of the fields into one field, formatting a couple of others, and deleting a few others. Then writing the result to an output file.
Here's the code:
Code: Select all
If ReadFile(0, sTempName)
FileBuffersSize(0, 65536)
If CreateFile(1, sOutputName19)
FileBuffersSize(1, 65536)
If CreateFile(2, sOutputName20)
FileBuffersSize(2, 65536)
Dim asInputData.s(26)
Dim asOutputData.s(24)
Dim alFieldLength(24)
SetGadgetText(#Gadget_fraProgress, "Processing: Preparing records")
qInputLength.q=Lof(0)
lOldPercent.l=-1
Repeat
DoEvents()
lNewPercent.l=Int(Loc(0)/qInputLength*100+0.5)
If lNewPercent<>lOldPercent
SetGadgetState(#Gadget_barProgress, lNewPercent)
lOldPercent=lNewPercent
EndIf
sInputLine.s=ReadString(0, #PB_Ascii)
If CountString(sInputLine, ",")=26
For lIndex=0 To 26
asInputData(lIndex)=StringField(sInputLine, lIndex+1, ",")
Next lIndex
If Val(asInputData(4))=0 Or Trim(asInputData(4))="999999999"
asInputData(4)=""
EndIf
If Val(asInputData(1))=5000 Or Val(asInputData(1))=5001 Or Val(asInputData(1))=8888
Continue
EndIf
If UCase(Trim(asInputData(5)))="UNKNOWN" And Len(Trim(asInputData(4)))=0
Continue
EndIf
If UCase(Trim(asInputData(5)))="AGGREGATE"
Continue
EndIf
asOutputData(0)=asInputData(0)
asOutputData(1)=RSet(Trim(Str(Val(asInputData(1)))), 5, "0")
asOutputData(2)=RSet(Trim(Str(Val(asInputData(2))%100)), 2, "0")
asOutputData(3)=RSet(Trim(Str(Val(asInputData(3)))), 5, "0")
If Len(Trim(asInputData(4)))
asOutputData(4)=RSet(Trim(asInputData(4)), 9, "0")
Else
asOutputData(4)=""
EndIf
If UCase(Trim(asInputData(8)))="P"
asOutputData(5)=""
For lIndex=5 To 7
asOutputData(5)+RemoveString(asInputData(lIndex), " ")+" "
Next lIndex
asOutputData(5)=Trim(asOutputData(5))
Else
asOutputData(5)=Trim(asInputData(5))
While FindString(asOutputData(5), " ", 1)
asOutputData(5)=ReplaceString(asOutputData(5), " ", " ")
Wend
EndIf
For lIndex=7 To 26
asOutputData(lIndex-2)=asInputData(lIndex)
Next lIndex
sOutputLine.s=""
For lIndex=0 To 24
lFieldLen=Len(Trim(asOutputData(lIndex)))
If lFieldLen>alFieldLength(lIndex)
alFieldLength(lIndex)=lFieldLen
EndIf
Select lIndex
Case 0
sOutputField.s=Trim(asOutputData(0))+",N,N"
Case 7, 14, 17, 18, 21
sOutputField=Trim(asOutputData(lIndex))
Case 24
sOutputField=Trim(asOutputData(24))+","+Chr(34)+Chr(34)
Default
sOutputField=Chr(34)+Trim(asOutputData(lIndex))+Chr(34)
EndSelect
If Len(Trim(sOutputLine))
sOutputLine+","
EndIf
sOutputLine+sOutputField
Next lIndex
If Val(asInputData(2))<2000
WriteStringN(1, sOutputLine, #PB_Ascii)
Else
WriteStringN(2, sOutputLine, #PB_Ascii)
EndIf
EndIf ; CountString
Until Eof(0) Or quitfrmUCPFilePrep
If CreateFile(3, GetPathPart(sOutputName)+"fieldlengths.txt")
For lIndex=0 To 24
WriteStringN(3, Str(lIndex)+". "+Str(alFieldLength(lIndex)))
Next lIndex
CloseFile(3)
EndIf
CloseFile(2)
quitfrmUCPFilePrep=1
EndIf ; CreateFile
CloseFile(1)
EndIf ; CreateFile
CloseFile(0)
EndIf ; ReadFileBTW: I saw references to Rings' FastFile library but it doesn't appear to be on any of the PureBasic support sites like PureProject or PureArea. Is it no longer available?