|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Globalization; |
| 5 | +using System.Text.RegularExpressions; |
| 6 | + |
| 7 | +public class PluginsEditorDummy |
| 8 | +{ |
| 9 | + // from http://www.dotnetperls.com/word-count |
| 10 | + |
| 11 | + public static int CountWords1(string s) |
| 12 | + { |
| 13 | + MatchCollection collection = Regex.Matches(s, @"[\S]+"); |
| 14 | + return collection.Count; |
| 15 | + } |
| 16 | + |
| 17 | + public static int CountWords2(string s) |
| 18 | + { |
| 19 | + int c = 0; |
| 20 | + for (int i = 1; i < s.Length; i++) |
| 21 | + { |
| 22 | + if (char.IsWhiteSpace(s[i - 1]) == true) |
| 23 | + { |
| 24 | + if (char.IsLetterOrDigit(s[i]) == true || |
| 25 | + char.IsPunctuation(s[i])) |
| 26 | + { |
| 27 | + c++; |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + if (s.Length > 2) |
| 32 | + { |
| 33 | + c++; |
| 34 | + } |
| 35 | + return c; |
| 36 | + } |
| 37 | + |
| 38 | + // from http://www.dotnetperls.com/array-optimization |
| 39 | + |
| 40 | + const int _max = 100000000; |
| 41 | + |
| 42 | + public static void ArrayOptimization() |
| 43 | + { |
| 44 | + int[] array = new int[12]; |
| 45 | + Method1(array); |
| 46 | + Method2(array); |
| 47 | + |
| 48 | + var s1 = Stopwatch.StartNew(); |
| 49 | + for (int i = 0; i < _max; i++) |
| 50 | + { |
| 51 | + Method1(array); |
| 52 | + } |
| 53 | + s1.Stop(); |
| 54 | + var s2 = Stopwatch.StartNew(); |
| 55 | + for (int i = 0; i < _max; i++) |
| 56 | + { |
| 57 | + Method2(array); |
| 58 | + } |
| 59 | + s2.Stop(); |
| 60 | + Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000 * 1000) / |
| 61 | + _max).ToString("0.00 ns")); |
| 62 | + Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000 * 1000) / |
| 63 | + _max).ToString("0.00 ns")); |
| 64 | + Console.Read(); |
| 65 | + } |
| 66 | + |
| 67 | + static void Method1(int[] array) |
| 68 | + { |
| 69 | + // Initialize each element in for-loop. |
| 70 | + for (int i = 0; i < array.Length; i++) |
| 71 | + { |
| 72 | + array[i] = 1; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + static void Method2(int[] array) |
| 77 | + { |
| 78 | + // Initialize each element in separate statement with no enclosing loop. |
| 79 | + array[0] = 1; |
| 80 | + array[1] = 1; |
| 81 | + array[2] = 1; |
| 82 | + array[3] = 1; |
| 83 | + array[4] = 1; |
| 84 | + array[5] = 1; |
| 85 | + array[6] = 1; |
| 86 | + array[7] = 1; |
| 87 | + array[8] = 1; |
| 88 | + array[9] = 1; |
| 89 | + array[10] = 1; |
| 90 | + array[11] = 1; |
| 91 | + } |
| 92 | + |
| 93 | + // from http://www.dotnetperls.com/dictionary |
| 94 | + |
| 95 | + public static void Dictionary1() |
| 96 | + { |
| 97 | + // Example Dictionary again. |
| 98 | + Dictionary<string, int> d = new Dictionary<string, int>() |
| 99 | + { |
| 100 | + {"cat", 2}, |
| 101 | + {"dog", 1}, |
| 102 | + {"llama", 0}, |
| 103 | + {"iguana", -1} |
| 104 | + }; |
| 105 | + // Loop over pairs with foreach. |
| 106 | + foreach (KeyValuePair<string, int> pair in d) |
| 107 | + { |
| 108 | + Console.WriteLine("{0}, {1}", |
| 109 | + pair.Key, |
| 110 | + pair.Value); |
| 111 | + } |
| 112 | + // Use var keyword to enumerate dictionary. |
| 113 | + foreach (var pair in d) |
| 114 | + { |
| 115 | + Console.WriteLine("{0}, {1}", |
| 116 | + pair.Key, |
| 117 | + pair.Value); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public static void Dictionary2() |
| 122 | + { |
| 123 | + Dictionary<string, int> d = new Dictionary<string, int>() |
| 124 | + { |
| 125 | + {"cat", 2}, |
| 126 | + {"dog", 1}, |
| 127 | + {"llama", 0}, |
| 128 | + {"iguana", -1} |
| 129 | + }; |
| 130 | + // Store keys in a List |
| 131 | + List<string> list = new List<string>(d.Keys); |
| 132 | + // Loop through list |
| 133 | + foreach (string k in list) |
| 134 | + { |
| 135 | + Console.WriteLine("{0}, {1}", |
| 136 | + k, |
| 137 | + d[k]); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + // from: http://www.dotnetperls.com/format |
| 142 | + |
| 143 | + public static void Format1() |
| 144 | + { |
| 145 | + // Declare three variables. |
| 146 | + // ... The values they have are not important. |
| 147 | + string value1 = "Dot Net Perls"; |
| 148 | + int value2 = 10000; |
| 149 | + DateTime value3 = new DateTime(2015, 11, 1); |
| 150 | + // Use string.Format method with four arguments. |
| 151 | + // ... The first argument is the formatting string. |
| 152 | + // ... It specifies how the next arguments are formatted. |
| 153 | + string result = string.Format("{0}: {1:0.0} - {2:yyyy}", |
| 154 | + value1, |
| 155 | + value2, |
| 156 | + value3); |
| 157 | + // Write the result. |
| 158 | + Console.WriteLine(result); |
| 159 | + } |
| 160 | + |
| 161 | + public static void Format2() |
| 162 | + { |
| 163 | + // Format a ratio as a percentage string. |
| 164 | + // ... You must specify the percentage symbol. |
| 165 | + // ... It will multiply the value by 100. |
| 166 | + double ratio = 0.73; |
| 167 | + string result = string.Format("string = {0:0.0%}", |
| 168 | + ratio); |
| 169 | + Console.WriteLine(result); |
| 170 | + } |
| 171 | + |
| 172 | + public static void Format3() |
| 173 | + { |
| 174 | + // The constant formatting string. |
| 175 | + // ... It specifies the padding. |
| 176 | + // ... A negative number means to left-align. |
| 177 | + // ... A positive number means to right-align. |
| 178 | + const string format = "{0,-10} {1,10}"; |
| 179 | + // Construct the strings. |
| 180 | + string line1 = string.Format(format, |
| 181 | + 100, |
| 182 | + 5); |
| 183 | + string line2 = string.Format(format, |
| 184 | + "Carrot", |
| 185 | + "Giraffe"); |
| 186 | + // Write the formatted strings. |
| 187 | + Console.WriteLine(line1); |
| 188 | + Console.WriteLine(line2); |
| 189 | + } |
| 190 | + |
| 191 | + public static void Format4() |
| 192 | + { |
| 193 | + int value1 = 10995; |
| 194 | + |
| 195 | + // Write number in hex format. |
| 196 | + Console.WriteLine("{0:x}", value1); |
| 197 | + Console.WriteLine("{0:x8}", value1); |
| 198 | + |
| 199 | + Console.WriteLine("{0:X}", value1); |
| 200 | + Console.WriteLine("{0:X8}", value1); |
| 201 | + |
| 202 | + // Convert to hex. |
| 203 | + string hex = value1.ToString("X8"); |
| 204 | + |
| 205 | + // Convert from hex to integer. |
| 206 | + int value2 = int.Parse(hex, NumberStyles.AllowHexSpecifier); |
| 207 | + Console.WriteLine(value1 == value2); |
| 208 | + } |
| 209 | +} |
0 commit comments