Help: Streamwriter Performance

Last post 07-04-2009 5:02 AM by Joker_Joker. 2 replies.

Sort Posts:

  • Help: Streamwriter Performance

    07-04-2009, 12:42 AM
    • Member
      2 point Member
    • Joker_Joker
    • Member since 05-24-2008, 8:26 AM
    • Posts 12

     

     Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    
            Dim FILENAME As String = Server.MapPath("Output2.txt")
    
            Dim objStreamWriter As StreamWriter = New StreamWriter(FILENAME, False, Encoding.ASCII, 10000000)
    
            Dim stringval As String
            Dim whitespace As String
            whitespace = 1
            For x = 4 To 100000
                whitespace = whitespace & x
            Next
            stringval = whitespace
    
            objStreamWriter.WriteLine(stringval)
    
            objStreamWriter.Flush()
            objStreamWriter.Close()
        
    
        End Sub


    The code above works, it outputs a file at 480 kb - taking 2 minutes to do so.

    What i'd like to ask the community is, could this process be cut down to less than 30 seconds or even better less than 10 seconds. I also like to add the actual output files i have are much larger than 400 KB, they are in the 1 to 2 MB (more than 100000 characters).

    At this moment, i would prefer sticking with this method to write out strings to a text file - however i am open to suggestions depending on how easy it is to migrate to the other method.

    As you notice, i have no idea why but i read some where specifying a large buffersize increases performance, however i don't notice this at all when i left it on default or not.

    Regards,
    Joker.

  • Re: Help: Streamwriter Performance

    07-04-2009, 1:05 AM
    Answer
    • All-Star
      30,502 point All-Star
    • HeartattacK
    • Member since 01-08-2007, 5:53 PM
    • Dhaka, Bangladesh
    • Posts 3,292
    • Moderator

     Well...your IO is pretty good...but there's a reason for the hideous performance. Strings in .net are immutable. Whenever you change a string, a new string is created. Done enough times, these pile up before garbage collection and hinders performance. Also, your stringval variable is pretty much unnecessary. Consider this bit of code instead:

    Dim whiteSpace As New StringBuilder()

    For x=4 To 100000

        whiteSpace.Append(x)

    Next

    objStreamWriter.WriteLine(whiteSpace.ToString())

    All that glitters is gold-
    Only shooting stars break the mold.

    Read my blog: www.heartysoft.com

    Tell me what tutorials / articles / videos you want to see on my site.
  • Re: Help: Streamwriter Performance

    07-04-2009, 5:02 AM
    • Member
      2 point Member
    • Joker_Joker
    • Member since 05-24-2008, 8:26 AM
    • Posts 12
            Dim getconnection As String = WebConfigurationManager.ConnectionStrings("DBConnect").ConnectionString
            Dim conn As New SqlConnection(getconnection)
            conn.Open()
    
            Dim filename As String = Server.MapPath("output56.txt")
            Dim writer As StreamWriter = New StreamWriter(filename, False, Encoding.ASCII, 500000)
            
            Dim sql As String = "SELECT [testname] FROM testtable"
            Dim getdata As New SqlCommand(sql, conn)
            Dim reader As SqlDataReader = getdata.ExecuteReader()
    
            Dim name As String
            Dim counter As String = 0
    
            While reader.Read()
            
                name = reader.GetString(0) & " this working?"
                writer.WriteLine(name)
                counter = counter + 1
                writer.WriteLine(counter)
    
            End While
    
            writer.Flush()
            writer.Close()
            reader.Close()
            conn.Close()
           


    Thanks for the help, above is the code i'm actually using (plus a lot more), i'm going to write an insert statement to put in 100,000 data to simulate what maybe a real time event.

    Then see how it performs.

    However, the current code - the sliced bit handles 10000 rows very nice, 1 second and done.

    I guess the above for loop was over kill in characters but thank you so much, i kept thinking it was streamwriter but now i know it's to do with the string. If i run into issues, i'll definately rework the way i'm using strings.

    You my friend, ROCK :)

    regards
    Joker

     

    * Edit *

    Just did this and beautiful - i actually tested my current code with some modifications and it created a 94 mb Text File

    Too bad i can't compress the text file down to 1 mb...but hey if anyone comes and reads this i would be glad to hear of any method to compress it to 1 mb (crosses fingers and prays??? lol ... i'd think not)

Page 1 of 1 (3 items)