Results 1 to 11 of 11

Thread: Normal vs. Stringbuilder Concatenation; unusual tick numbers

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    8

    Normal vs. Stringbuilder Concatenation; unusual tick numbers

    StringBuilder.zip

    This is the objective of my assignment:
    "This exercise explores the merits of using Stringbuilder to perform string manipulations, in particular, concatenations. Strings are reference objects and immutable. Modifications To a string of any kind require that a new string be created, and that the revised contents of the old string be copied to the location of the new string. As you can imagine, this process can quickly become very costly in terms of resources. The solution is to use StringBuilder, which allocates a buffer instead, and makes the changes in place. StringBuilder is not a string but rather a workspace. The internal string is easily extracted from StringBuilder buffer using the ToString()method."

    When I execute the app, I get a number of ticks for normal string concatenation but, string builder concatenation, I get 0 ticks and for difference, I get infinite ticks. When I repeatedly click the go button, I eventually get a number for stringbuilder concatenation and difference, but once I click it again, I get 0 and infinite again. Repeat that and same results.

    This code was given by the professor. Can you tell me if its intentional or if there's a problem with the code? If so, how can I fix it?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Normal vs. Stringbuilder Concatenation; unusual tick numbers

    Can you post the code?
    The StringBuilder would be quicker, and depending on how much you're using it, it could be very fast...

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    8

    Re: Normal vs. Stringbuilder Concatenation; unusual tick numbers

    Code:
    Public Class frmStringBuilder
    
        Private Sub btnGo_Click(sender As Object, e As EventArgs) Handles btnGo.Click
    
            Dim str As String
            Dim strAll As String
            Dim sb As New System.Text.StringBuilder
            Dim intIterations As Integer = CInt(updIterations.Value)
            Dim intCtr As Integer
            Dim lngStart, lngFinish As Long
            Dim lngDiff1, lngDiff2 As Long
    
            lblNormal.Text = ""
            lblStringBuilder.Text = ""
            lblDifference.Text = ""
            Me.Cursor = Cursors.WaitCursor
            strAll = String.Empty
    
            '* Normal Concatenation
            str = txtString.Text & " "
            lngStart = Now.Ticks
    
            For intCtr = 0 To intIterations
                strAll = strAll & str
            Next
    
            lngFinish = Now.Ticks
            lngDiff1 = (lngFinish - lngStart)
            lblNormal.Text = lngDiff1.ToString("N0") & " ticks"
            Debug.WriteLine(strAll.Length.ToString())
            sb.EnsureCapacity(intIterations * txtString.Text.Length)
    
            '* Stringbuilder Concatenation
            lngStart = Now.Ticks
    
            For intCtr = 0 To intIterations
                sb.Append(str)
            Next
    
            lngFinish = Now.Ticks
            lngDiff2 = (lngFinish - lngStart)
            lblStringBuilder.Text = lngDiff2.ToString("N0") & " ticks"
            Debug.WriteLine(sb.ToString.Length.ToString())
    
            '* Difference
            lblDifference.Text = String.Format("{0:N0}", lngDiff1 / lngDiff2) & " times"
            sb = Nothing
            Me.Cursor = Cursors.Default
    
        End Sub
    
    End Class

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Normal vs. Stringbuilder Concatenation; unusual tick numbers

    What sort of number of iterations are we talking about? try it with 100,000

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    8

    Re: Normal vs. Stringbuilder Concatenation; unusual tick numbers

    Ok, 100,000 gives results, but 10,000 give 0 and infinite

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Normal vs. Stringbuilder Concatenation; unusual tick numbers

    What happens if you run it for 10,000 iterations, then display the contents of the stringbuilder into a textbox?
    If it's what it should be, everything must be working ok, so report your findings to your teacher. I did notice you're setting the size of the stringbuilder before any iterations occur. That would reduce the time taken to add the strings significantly.

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    8

    Re: Normal vs. Stringbuilder Concatenation; unusual tick numbers

    ok, thanks

  8. #8
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Normal vs. Stringbuilder Concatenation; unusual tick numbers

    Today's computers are so fast you cannot measure the time it takes to do one operation. A tick is some fraction of a second, and your CPU cycles are fractions of Ticks. So the time it takes to do most operations is less than a single Tick, which means 0 in most cases.

    So when you're benchmarking things, it's best to execute them for hundreds of thousands or millions of iterations, so all of the fractions of a tick can add up to whole ticks. Also: DateTime.Now is not a high-precision clock. Read the documentation for System.Diagnostics.Stopwatch. That is a much more high-precision clock intended to be used for measuring the performance of algorithms.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    8

    Re: Normal vs. Stringbuilder Concatenation; unusual tick numbers

    Thanks for the explanation

  10. #10
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Normal vs. Stringbuilder Concatenation; unusual tick numbers

    If that's the performance testing code you've been given, i'd mention it's not the most efficient code for measuring time taken to complete a set of iterations.
    In addition to the StopWatch class, i generally use Environment.TickCount

    https://msdn.microsoft.com/en-us/lib...or=-2147217396

  11. #11

    Thread Starter
    New Member
    Join Date
    Apr 2017
    Posts
    8

    Re: Normal vs. Stringbuilder Concatenation; unusual tick numbers

    Ok, yeah, that looks interesting.

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