Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. he he

he he

Scheduled Pinned Locked Moved The Weird and The Wonderful
com
13 Posts 8 Posters 8 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Sylvester george

    string currentdate = "Current date and time is " + DateTime.Now.ToString(); StringBuilder sbcurrentdate = new StringBuilder(); sbcurrentdate.Append(currentdate); return sbcurrentdate;

    Regards, Sylvester G sylvester_g_m@yahoo.com

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #4

    I don't get it.:doh: Oh well...

    B 1 Reply Last reply
    0
    • L Lost User

      I don't get it.:doh: Oh well...

      B Offline
      B Offline
      benjymous
      wrote on last edited by
      #5

      The whole point of using StringBuilder is that it's far more efficient than using "+" to "add" two strings together - the programmer was probably told to use StringBuilder for efficiency, and that was what they came up with

      S L 2 Replies Last reply
      0
      • B benjymous

        The whole point of using StringBuilder is that it's far more efficient than using "+" to "add" two strings together - the programmer was probably told to use StringBuilder for efficiency, and that was what they came up with

        S Offline
        S Offline
        Sylvester george
        wrote on last edited by
        #6

        you caught the code nicely

        Regards, Sylvester G sylvester_g_m@yahoo.com

        1 Reply Last reply
        0
        • B benjymous

          The whole point of using StringBuilder is that it's far more efficient than using "+" to "add" two strings together - the programmer was probably told to use StringBuilder for efficiency, and that was what they came up with

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #7

          Oh...:~ Well, that kind of makes a bit more sense now, but I still don't see what the big deal is. Other than the uselessness of StringBuilder, everything seems fine to me. Maybe if there were a lot more "+" concatenations before the StringBuilder usage, it would be a little funnier. Also, I never trust an object to be better than another or than simple operations just because the documentation says so. There are a lot of ways you can concatenate two strings but the simplest and fastest way should always be the obvious way... However, if you can prove that StringBuilder is better than simple string concatenation, you get an A+. :D

          D T 2 Replies Last reply
          0
          • L Lost User

            Oh...:~ Well, that kind of makes a bit more sense now, but I still don't see what the big deal is. Other than the uselessness of StringBuilder, everything seems fine to me. Maybe if there were a lot more "+" concatenations before the StringBuilder usage, it would be a little funnier. Also, I never trust an object to be better than another or than simple operations just because the documentation says so. There are a lot of ways you can concatenate two strings but the simplest and fastest way should always be the obvious way... However, if you can prove that StringBuilder is better than simple string concatenation, you get an A+. :D

            D Offline
            D Offline
            DarrollWalsh
            wrote on last edited by
            #8

            It should read like so

            return new StringBuilder("Current date and time is " + DateTime.Now.ToString());

            Lots of useless code to return a string as a StringBuilder. Or, shouldn't have used StringBuilder at all. The object of the StringBuilder class is to save memory when appending strings together. The original code misses that concept and uses more memory than if he would have just added the strings together.

            Darroll Not one person lives in the present.

            1 Reply Last reply
            0
            • L Lost User

              Oh...:~ Well, that kind of makes a bit more sense now, but I still don't see what the big deal is. Other than the uselessness of StringBuilder, everything seems fine to me. Maybe if there were a lot more "+" concatenations before the StringBuilder usage, it would be a little funnier. Also, I never trust an object to be better than another or than simple operations just because the documentation says so. There are a lot of ways you can concatenate two strings but the simplest and fastest way should always be the obvious way... However, if you can prove that StringBuilder is better than simple string concatenation, you get an A+. :D

              T Offline
              T Offline
              TwoFaced
              wrote on last edited by
              #9

              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+? :)

              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

              L S 3 Replies Last reply
              0
              • T TwoFaced

                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+? :)

                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

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #10

                :)

                1 Reply Last reply
                0
                • T TwoFaced

                  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+? :)

                  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

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #11

                  It's all done with mirrors.;)

                  1 Reply Last reply
                  0
                  • T TwoFaced

                    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+? :)

                    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

                    S Offline
                    S Offline
                    Sylvester george
                    wrote on last edited by
                    #12

                    Horring me :confused::confused::confused::confused::confused::confused::rolleyes::mad:

                    Regards, Sylvester G sylvester_g_m@yahoo.com

                    1 Reply Last reply
                    0
                    • S Sylvester george

                      string currentdate = "Current date and time is " + DateTime.Now.ToString(); StringBuilder sbcurrentdate = new StringBuilder(); sbcurrentdate.Append(currentdate); return sbcurrentdate;

                      Regards, Sylvester G sylvester_g_m@yahoo.com

                      S Offline
                      S Offline
                      Sathesh Sakthivel
                      wrote on last edited by
                      #13

                      :laugh::laugh::laugh:

                      Regards, Satips.:rose: Don't walk in front of me, I may not follow; Don't walk behind me, I may not lead; Walk beside me, and just be my friend. - Albert Camus

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups