Results 1 to 7 of 7

Thread: Any Suggestions for Making this More Efficient?

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    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:
    1. Private Function createzonesFedEx(ByVal Filename As String, ByVal ShipType As String, ByVal Carrier As String)
    2.         Dim dash As Boolean 'determines whether or not the first column of numbers contain a dash
    3.         Dim i As Integer
    4.         Dim line As String 'line of data to be read
    5.         Dim commaArray() As String 'array of strings delimited by commas
    6.         Dim dashArray() As String ' array of strings delimited by dashes
    7.         Dim sr = New System.IO.StreamReader(Filename) ' I/O object to read data from Filename
    8.         Dim sw = New System.IO.StreamWriter(FILEPATH, True) 'I/O object to write data to FILEPATH
    9.  
    10.         line = sr.readline
    11.         While Not line Is Nothing
    12.  
    13.             commaArray = Split(line, ",")
    14.  
    15.             If (Mid(commaArray(0), 4, 1) = "-") Then
    16.                 dashArray = Split(commaArray(LBound(commaArray)), "-")
    17.                 dash = False
    18.             Else
    19.                 dash = True
    20.             End If
    21.  
    22.             For i = 0 To 7 Step 1
    23.                 Me.cboShipType.SelectedIndex = i
    24.                 Try
    25.                     sw.write(FRFIRM)
    26.                     sw.write(",")
    27.                     sw.write(Me.cboCarrier.Text)
    28.                     sw.write(",")
    29.                     sw.write(Me.cboShipType.Text)
    30.                     sw.write(",")
    31.                     sw.write(FVRSN)'Global constant
    32.                     sw.write(",")
    33.                     If (dash = False) Then
    34.                         sw.write(Format(Val(Trim(dashArray(0))), "000"))
    35.                         sw.write(",")
    36.                         sw.write(Format(Val(Trim(dashArray(1))), "000"))
    37.                         sw.write(",")
    38.                         sw.write(Format(Val(Trim(dashArray(1))), "000"))
    39.                     Else
    40.                         sw.write(Format(Val(Trim(commaArray(0))), "000"))
    41.                         sw.write(",")
    42.                         sw.write(Format(Val(Trim(commaArray(0))), "000"))
    43.                         sw.write(",")
    44.                         sw.write(Format(Val(Trim(commaArray(0))), "000"))
    45.                     End If
    46.                     sw.write(",")
    47.                     sw.write(FRSTA)'Global constant
    48.                     sw.write(",")
    49.                     sw.write(Now.Today.ToString("yyyyMMdd"))
    50.                     sw.write(",")
    51.                     sw.write(Now.Today.ToString("yyyyMMdd"))
    52.                     sw.write(",")
    53.                     sw.write(FRZAEN)'Global constant
    54.                     sw.write(",")
    55.                     sw.write(FRUSER)'Global constant
    56.                     sw.write(",")
    57.                     sw.writeline(FRWSID)'Global constant
    58.                     sw.newline()
    59.                 Catch ex As Exception
    60.                     MessageBox.Show(ex.Message)
    61.                 End Try
    62.             Next
    63.             line = sr.readline
    64.         End While
    65.         Me.cboShipType.SelectedIndex = -1
    66.         sw.Close()
    67.     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.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    Sep 2005
    Posts
    200

    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.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  6. #6
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Any Suggestions for Making this More Efficient?

    Read the whole file into memory by splitting line by line

    VB Code:
    1. Dim sr As New System.IO.StreamReader("filename")
    2.         Dim str As String = sr.ReadToEnd()
    3.         'this will split each line and put it in memory
    4.         Dim strAry() As String = str.Replace(Chr(10), "").Split(Chr(13))
    This takes away having to call ReadLine() over and over.

  7. #7
    Fanatic Member MetalKid's Avatar
    Join Date
    Aug 2005
    Location
    Green Bay, Wisconsin
    Posts
    534

    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
  •  



Click Here to Expand Forum to Full Width