-
Oct 15th, 2017, 07:16 PM
#1
Thread Starter
New Member
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?
-
Oct 15th, 2017, 07:34 PM
#2
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...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 15th, 2017, 07:36 PM
#3
Thread Starter
New Member
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
-
Oct 15th, 2017, 07:51 PM
#4
Re: Normal vs. Stringbuilder Concatenation; unusual tick numbers
What sort of number of iterations are we talking about? try it with 100,000
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 15th, 2017, 08:01 PM
#5
Thread Starter
New Member
Re: Normal vs. Stringbuilder Concatenation; unusual tick numbers
Ok, 100,000 gives results, but 10,000 give 0 and infinite
-
Oct 15th, 2017, 08:25 PM
#6
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 15th, 2017, 08:29 PM
#7
Thread Starter
New Member
Re: Normal vs. Stringbuilder Concatenation; unusual tick numbers
-
Oct 15th, 2017, 08:53 PM
#8
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.
-
Oct 15th, 2017, 09:02 PM
#9
Thread Starter
New Member
Re: Normal vs. Stringbuilder Concatenation; unusual tick numbers
Thanks for the explanation
-
Oct 15th, 2017, 09:28 PM
#10
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
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 15th, 2017, 10:52 PM
#11
Thread Starter
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|