25 | | For more information on the StringBuilder class, see [http://www.google.com/search?hl=en&q=C%23+stringbuilder Google(C# StringBuilder)] |
| 25 | The vernacular notation is something like |
| 26 | |
| 27 | {{{ |
| 28 | sb = StringBuilder() |
| 29 | seperator = '' |
| 30 | for <someloop> |
| 31 | value = ... some calc or marshalling |
| 32 | sb.append(sep) |
| 33 | sep = ',' # or whatever separator is between items |
| 34 | sb.append(value) |
| 35 | s = sb.toString # turn into a string for subsequent use |
| 36 | }}} |
| 37 | |
| 38 | = A note about performance = |
| 39 | With string immutability and wrt .Net/C# conventional wisdom has it that use of a !StringBuilder is faster/better performance wise than just catenating to a String type but it depends.... |
| 40 | |
| 41 | * True, !StringBuilder append is 915-2000x faster than String append but for small strings the overhead of !StringBuilder create |
| 42 | (and conversion back to String) is much higher (150-180x) than just String create/append... |
| 43 | * Putting a single string in a !StringBuilder is about 1.6x speed of String doing the same. |
| 44 | - i.e String append ( {{{ s='...' s+='...'...}}} ) is FASTER than ( {{{ sb=StringBuilder('...' s.append('...' ...}}} ) |
| 45 | |
| 46 | * create/append...2 strings in !StringBuilder about same (1.02) speed of String doing same |
| 47 | * create/append...3,4 strings in !StringBuilder faster (0.92, .8) speed than that of String doing same |
| 48 | (Without conversion !StringBuilder back to String) |
| 49 | |
| 50 | Implies use String if appending <=2 strings, !StringBuilder for more and using result directly[[BR]] |
| 51 | |
| 52 | Full Cycle: create, append, convert to String[[BR]] |
| 53 | * create, convert to string - using String is about 200x faster than using a !StringBuilder |
| 54 | * create, append 1 string, convert to String - String about 1.9x faster than !StringBuilder |
| 55 | - conversion !StringBuilder to String overhead is about 0.3 |
| 56 | * create, append 2 strings, convert to String - using String about 1.13x faster than using !StringBuilder |
| 57 | * create, append 3 strings, convert to String - using String about 1.2x faster than !StringBuilder |
| 58 | * create, append 4 strings, convert to String - using String about same (.96, 1.04 slower) speed as using !StringBuilder |
| 59 | * create, append 5 strings, convert to string - using String about 0.86 (1.2 slower) than !StringBuilder |
| 60 | |
| 61 | Implies that it is faster to use String if expecting to append <=4 strings, otherwise use a !StringBuilder ... |
| 62 | |
| 63 | |
| 64 | For more information on the StringBuilder class, see [http://www.google.com/search?hl=en&q=C%23+stringbuilder Google(C# !StringBuilder)] |