Click here to Skip to main content
15,886,689 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: he he Pin
Optimus Chaos5-Jun-07 2:04
Optimus Chaos5-Jun-07 2:04 
GeneralRe: he he Pin
DavidNohejl5-Jun-07 2:11
DavidNohejl5-Jun-07 2:11 
GeneralRe: he he Pin
Emil - Gabriel11-Jun-07 4:09
Emil - Gabriel11-Jun-07 4:09 
GeneralRe: he he Pin
benjymous12-Jun-07 0:21
benjymous12-Jun-07 0:21 
GeneralRe: he he Pin
Sylvester george12-Jun-07 1:14
Sylvester george12-Jun-07 1:14 
GeneralRe: he he Pin
Emil - Gabriel12-Jun-07 1:54
Emil - Gabriel12-Jun-07 1:54 
GeneralRe: he he Pin
DarrollWalsh19-Jun-07 5:20
DarrollWalsh19-Jun-07 5:20 
GeneralRe: he he [modified] Pin
TwoFaced20-Jun-07 8:58
TwoFaced20-Jun-07 8:58 
sk8er_boy287 wrote:
However, if you can prove that StringBuilder is better than simple string concatenation, you get an A+.
If you need to build a string in a loop the stringbuilder is FAR more efficient then normal string concatenation. I've run into the problem before and learned the hard way. Actually I learned about it a while back using VB6, but VB6 didn't have a nice StringBuilder so I figured out another way around it. A simple example will show the difference. Do I get an A+? Smile | :)
Public Class Form1
    Private Sub TestStringConcatenation(ByVal loops As Integer, ByVal myText As String)
        Dim s As String = ""
        For i As Integer = 0 To loops
            s &= myText
        Next
    End Sub

    Private Sub TestStringBuilder(ByVal loops As Integer, ByVal myText As String)
        Dim sb As New System.Text.StringBuilder
        For i As Integer = 0 To loops
            sb.Append(myText)
        Next
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Const LOOPS As Integer = 1000
        Dim myText As String = New String("A"c, 200)
        Dim sw As New Stopwatch

        sw.Start()
        TestStringConcatenation(LOOPS, myText)
        sw.Stop()
        Console.WriteLine("Normal string concatenation took {0} ticks", sw.ElapsedTicks)

        sw.Reset()
        sw.Start()
        TestStringBuilder(LOOPS, myText)
        sw.Stop()
        Console.WriteLine("String builder took {0} ticks", sw.ElapsedTicks)
    End Sub
End Class
As for the code the OP posted, what's funny about it is how much extra useless code the person used to return a string. All that was needed was a simple line
Return "Current date and time is " + DateTime.Now.ToString()
As someone already mentioned the original code writer probably learned that is was more efficient to use the stringbuilder if you are going to append text to a string. Unfortunatly they didn't use it correctly and didn't really grasp when it's appropriate to use it. I ran a test to see what method performed best. In this case normal string concatenation seemed to out perform stringbuilder. Here is my code I used to see the performance of each method.
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Const TRIALS As Integer = 1000
        Const LOOPS As Integer = 1000

        Dim totalTicks As Long = 0

        For i As Integer = 0 To TRIALS - 1
            Dim sw As New Stopwatch
            sw.Start()
            For x As Integer = 0 To LOOPS - 1
                Dim s As String = StringBuilderUsage1()
            Next
            sw.Stop()
            totalTicks += sw.ElapsedTicks
        Next
        Console.WriteLine("Average ticks for StringBuilderUsage1 = " & (totalTicks / TRIALS).ToString)

        totalTicks = 0
        For i As Integer = 0 To TRIALS - 1
            Dim sw As New Stopwatch
            sw.Start()
            For x As Integer = 0 To LOOPS - 1
                Dim s As String = StringBuilderUsage2()
            Next
            sw.Stop()
            totalTicks += sw.ElapsedTicks
        Next
        Console.WriteLine("Average ticks for StringBuilderUsage2 = " & (totalTicks / TRIALS).ToString)

        totalTicks = 0
        For i As Integer = 0 To TRIALS - 1
            Dim sw As New Stopwatch
            sw.Start()
            For x As Integer = 0 To LOOPS - 1
                Dim s As String = NormalConcatenation()
            Next
            sw.Stop()
            totalTicks += sw.ElapsedTicks
        Next
        Console.WriteLine("Average ticks for normal concatenation = " & (totalTicks / TRIALS).ToString)
    End Sub

    Private Function StringBuilderUsage1() As String
        Dim sb As New System.Text.StringBuilder
        sb.Append("Current date and time is ")
        sb.Append(Now.ToLongTimeString)
        Return sb.ToString
    End Function

    Private Function StringBuilderUsage2() As String
        Return New System.Text.StringBuilder("Current date and time is " & Now.ToLongTimeString).ToString
    End Function

    Private Function NormalConcatenation() As String
        Return "Current date and time is " & Now.ToLongTimeString
    End Function
End Class
It's a rainy day here, so I had the time for all this nonsense Smile | :)


-- modified at 15:22 Wednesday 20th June, 2007
GeneralRe: he he Pin
Emil - Gabriel20-Jun-07 20:26
Emil - Gabriel20-Jun-07 20:26 
GeneralRe: he he Pin
Emil - Gabriel20-Jun-07 20:27
Emil - Gabriel20-Jun-07 20:27 
GeneralRe: he he Pin
Sylvester george21-Jun-07 20:43
Sylvester george21-Jun-07 20:43 
GeneralRe: he he Pin
Sathesh Sakthivel9-Jul-07 20:05
Sathesh Sakthivel9-Jul-07 20:05 
JokeHa ha Pin
ScottM13-Jun-07 23:08
ScottM13-Jun-07 23:08 
GeneralRe: Ha ha Pin
Giorgi Dalakishvili3-Jun-07 23:34
mentorGiorgi Dalakishvili3-Jun-07 23:34 
GeneralRe: Ha ha Pin
ScottM13-Jun-07 23:41
ScottM13-Jun-07 23:41 
GeneralRe: Ha ha Pin
Daniel Grunwald5-Jun-07 3:32
Daniel Grunwald5-Jun-07 3:32 
GeneralRe: Ha ha Pin
Dan Neely5-Jun-07 3:46
Dan Neely5-Jun-07 3:46 
GeneralRe: Ha ha Pin
Daniel Grunwald5-Jun-07 4:04
Daniel Grunwald5-Jun-07 4:04 
GeneralRe: Ha ha Pin
KarstenK4-Jun-07 1:46
mveKarstenK4-Jun-07 1:46 
GeneralRe: Ha ha Pin
dighn4-Jun-07 10:47
dighn4-Jun-07 10:47 
GeneralRe: Ha ha [modified] Pin
Sameer Alibhai4-Jun-07 13:16
Sameer Alibhai4-Jun-07 13:16 
GeneralRe: Ha ha Pin
Rage5-Jun-07 21:11
professionalRage5-Jun-07 21:11 
GeneralRe: Ha ha Pin
Sameer Alibhai6-Jun-07 2:09
Sameer Alibhai6-Jun-07 2:09 
GeneralRe: Ha ha Pin
jhwurmbach5-Jun-07 23:08
jhwurmbach5-Jun-07 23:08 
GeneralRe: Casting true to true! Pin
Sameer Alibhai6-Jun-07 2:11
Sameer Alibhai6-Jun-07 2:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.