|
-
Feb 26th, 2007, 02:08 PM
#1
Thread Starter
Addicted Member
Any Suggestions for Making this More Efficient?
I'd like to see if anyone has any suggestions on making the following function faster or more efficient. This code segment reads data from a CVS file, breaks it up and writes the relevant data to a flat file. Some sample input would be:
Column A Column B
004-005 5
35 6
010-034 5
VB Code:
Private Function createzonesFedEx(ByVal Filename As String, ByVal ShipType As String, ByVal Carrier As String)
Dim dash As Boolean 'determines whether or not the first column of numbers contain a dash
Dim i As Integer
Dim line As String 'line of data to be read
Dim commaArray() As String 'array of strings delimited by commas
Dim dashArray() As String ' array of strings delimited by dashes
Dim sr = New System.IO.StreamReader(Filename) ' I/O object to read data from Filename
Dim sw = New System.IO.StreamWriter(FILEPATH, True) 'I/O object to write data to FILEPATH
line = sr.readline
While Not line Is Nothing
commaArray = Split(line, ",")
If (Mid(commaArray(0), 4, 1) = "-") Then
dashArray = Split(commaArray(LBound(commaArray)), "-")
dash = False
Else
dash = True
End If
For i = 0 To 7 Step 1
Me.cboShipType.SelectedIndex = i
Try
sw.write(FRFIRM)
sw.write(",")
sw.write(Me.cboCarrier.Text)
sw.write(",")
sw.write(Me.cboShipType.Text)
sw.write(",")
sw.write(FVRSN)'Global constant
sw.write(",")
If (dash = False) Then
sw.write(Format(Val(Trim(dashArray(0))), "000"))
sw.write(",")
sw.write(Format(Val(Trim(dashArray(1))), "000"))
sw.write(",")
sw.write(Format(Val(Trim(dashArray(1))), "000"))
Else
sw.write(Format(Val(Trim(commaArray(0))), "000"))
sw.write(",")
sw.write(Format(Val(Trim(commaArray(0))), "000"))
sw.write(",")
sw.write(Format(Val(Trim(commaArray(0))), "000"))
End If
sw.write(",")
sw.write(FRSTA)'Global constant
sw.write(",")
sw.write(Now.Today.ToString("yyyyMMdd"))
sw.write(",")
sw.write(Now.Today.ToString("yyyyMMdd"))
sw.write(",")
sw.write(FRZAEN)'Global constant
sw.write(",")
sw.write(FRUSER)'Global constant
sw.write(",")
sw.writeline(FRWSID)'Global constant
sw.newline()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Next
line = sr.readline
End While
Me.cboShipType.SelectedIndex = -1
sw.Close()
End Function
This works fine, I just think it could be done better. Any suggestions? Thank you for your time.
Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.
-
Feb 26th, 2007, 02:11 PM
#2
Thread Starter
Addicted Member
Re: Any Suggestions for Making this More Efficient?
It didn't turn out quite like I wanted it to, so to clarify: Column A is the 004-005, 35, 010-034 and Column B is 5,6,5.
Nenio foriras ĝis ĝi havas instru ni kiu ni devas scii.
-
Feb 26th, 2007, 05:12 PM
#3
Re: Any Suggestions for Making this More Efficient?
What's the problem with it? I can see a few simple little changes that might save the blink of an eye, but nothing that would save considerable time.
My usual boring signature: Nothing
 
-
Feb 26th, 2007, 05:14 PM
#4
Re: Any Suggestions for Making this More Efficient?
I have a few suggestions.
1. If the input csv file isn't too big, try read the whole file to memory at once.
2. Why you have to loop thru 0-7 and set the combobox's selectedindex for every line in the csv file just to read its text?
3. Use stringbuilder to build your output, then once all done, write it to the output file.
-
Feb 26th, 2007, 05:43 PM
#5
Re: Any Suggestions for Making this More Efficient?
Actually, changing the index on the comboboxes was the one thing which I thought might make a significant improvement. It would be better to just get the items from the combobox directly rather than making it the selected index, then getting the selected item. However, I don't know whether or not that would make a noticeable improvement in speed.
My usual boring signature: Nothing
 
-
Feb 26th, 2007, 06:10 PM
#6
Frenzied Member
Re: Any Suggestions for Making this More Efficient?
Read the whole file into memory by splitting line by line
VB Code:
Dim sr As New System.IO.StreamReader("filename")
Dim str As String = sr.ReadToEnd()
'this will split each line and put it in memory
Dim strAry() As String = str.Replace(Chr(10), "").Split(Chr(13))
This takes away having to call ReadLine() over and over.
-
Feb 26th, 2007, 06:15 PM
#7
Fanatic Member
Re: Any Suggestions for Making this More Efficient?
The easiest thing to do would just append that text to a System.Text.StringBuilder and then write the whole thing to the writer at the end instead of hitting it every line.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|