SHARE
TWEET

StringBuilder vs String.Concat

kwp Apr 3rd, 2015 259 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // In response to: http://trelford.com/blog/post/StringBuilder.aspx
  2.  
  3. open System
  4. open System.Text
  5.  
  6. let random, sb, n = new Random(), new StringBuilder(), 100000000
  7.  
  8. let inline getstring length =
  9.         new String(Array.init length (fun _ -> random.Next(97, 123) |> char))
  10.  
  11. #time "on"
  12.  
  13. for i = 1 to n do
  14.         String.Concat(getstring(6), getstring(6), getstring(6), getstring(6))
  15.         |> ignore
  16.  
  17. #time "on"
  18.        
  19. for i = 1 to n do
  20.         sb.Length <- 0
  21.         sb.Append(getstring(6)).Append(getstring(6)).Append(getstring(6)).Append(getstring(6)).ToString()
  22.         |> ignore
  23. ;;
  24.  
  25. /*
  26. ITERATIONS: 100
  27.         CONCAT
  28.         Real: 00:00:00.001, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
  29.         val it : unit = ()
  30.        
  31.         STRINGBUILDER
  32.         Real: 00:00:00.001, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
  33.         val it : unit = ()
  34.  
  35. ITERATIONS: 1000
  36.         CONCAT
  37.         Real: 00:00:00.001, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
  38.         val it : unit = ()
  39.        
  40.         STRINGBUILDER
  41.         Real: 00:00:00.001, CPU: 00:00:00.015, GC gen0: 0, gen1: 0, gen2: 0
  42.         val it : unit = ()
  43.        
  44. ITERATIONS: 100'000
  45.         CONCAT
  46.         Real: 00:00:00.078, CPU: 00:00:00.078, GC gen0: 10, gen1: 3, gen2: 1
  47.         val it : unit = ()
  48.        
  49.         STRINGBUILDER
  50.         Real: 00:00:00.045, CPU: 00:00:00.046, GC gen0: 9, gen1: 1, gen2: 0
  51.         val it : unit = ()
  52.        
  53. ITERATIONS: 1'000'000
  54.         CONCAT
  55.         Real: 00:00:00.430, CPU: 00:00:00.436, GC gen0: 95, gen1: 3, gen2: 1
  56.         val it : unit = ()
  57.        
  58.         STRINGBUILDER
  59.         Real: 00:00:00.413, CPU: 00:00:00.421, GC gen0: 95, gen1: 1, gen2: 0
  60.         val it : unit = ()
  61.        
  62. ITERATIONS: 100'000'000
  63.         CONCAT
  64.         Real: 00:00:39.005, CPU: 00:00:39.000, GC gen0: 9537, gen1: 6, gen2: 1
  65.         val it : unit = ()
  66.        
  67.         STRINGBUILDER
  68.         Real: 00:00:40.887, CPU: 00:00:40.841, GC gen0: 9537, gen1: 5, gen2: 0
  69.         val it : unit = ()
  70. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top