SHARE
TWEET

StringBuilder vs. String.Concat (2)

kwp Apr 3rd, 2015 237 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. open System
  2. open System.Text
  3.  
  4. type Thing() = class
  5.         member val Name = "Something" with get
  6.         override this.ToString() = this.Name
  7. end
  8.  
  9. let random, sb, n, thing =
  10.         new Random(),
  11.         new StringBuilder(),
  12.         100,
  13.         new Thing()
  14.  
  15. #time "on"
  16.  
  17. for i = 1 to n do
  18.         String.Concat(thing.ToString(), thing.ToString(), thing.ToString(), thing.ToString(), thing.ToString())
  19.         |> ignore
  20.  
  21. #time "on"
  22.        
  23. for i = 1 to n do
  24.         sb.Length <- 0
  25.         sb.Append(thing.ToString()).Append(thing.ToString()).Append(thing.ToString()).Append(thing.ToString()).ToString()
  26.         |> ignore
  27. ;;
  28.  
  29. /*
  30. ITERATIONS: 100
  31.         --> CONCAT
  32.         Real: 00:00:00.000, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
  33.         val it : unit = ()
  34.  
  35.         --> STRINGBUILDER
  36.         Real: 00:00:00.000, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
  37.         val it : unit = ()
  38.  
  39. ITERATIONS: 1000
  40.         --> CONCAT
  41.         Real: 00:00:00.000, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
  42.         val it : unit = ()
  43.  
  44.         --> STRINGBUILDER
  45.         Real: 00:00:00.000, CPU: 00:00:00.000, GC gen0: 0, gen1: 0, gen2: 0
  46.         val it : unit = ()
  47.        
  48. ITERATIONS: 100'000
  49.         --> CONCAT
  50.         Real: 00:00:00.057, CPU: 00:00:00.062, GC gen0: 6, gen1: 3, gen2: 1
  51.         val it : unit = ()
  52.  
  53.         --> STRINGBUILDER
  54.         Real: 00:00:00.009, CPU: 00:00:00.015, GC gen0: 2, gen1: 0, gen2: 0
  55.         val it : unit = ()
  56.        
  57. ITERATIONS: 1'000'000
  58.         --> CONCAT
  59.         Real: 00:00:00.142, CPU: 00:00:00.156, GC gen0: 63, gen1: 3, gen2: 1
  60.         val it : unit = ()
  61.  
  62.         --> STRINGBUILDER
  63.         Real: 00:00:00.064, CPU: 00:00:00.062, GC gen0: 24, gen1: 0, gen2: 0
  64.         val it : unit = ()
  65.        
  66. ITERATIONS: 100'000'000
  67.         --> CONCAT
  68.         Real: 00:00:09.612, CPU: 00:00:09.609, GC gen0: 6294, gen1: 3, gen2: 1
  69.         val it : unit = ()
  70.  
  71.         --> STRINGBUILDER - 30% FASTER
  72.         Real: 00:00:06.321, CPU: 00:00:06.318, GC gen0: 2479, gen1: 1, gen2: 0
  73.         val it : unit = ()
  74. */
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