Guest User

Untitled

a guest
Jun 20th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.57 KB | None | 0 0
  1. 1 if (myString.Trim().Length == 0)
  2. 2 if (myString.Trim() == "")
  3. 3 if (myString.Trim().Equals(""))
  4. 4 if (myString.Trim() == String.Empty)
  5. 5 if (myString.Trim().Equals(String.Empty))
  6.  
  7. Test orders:
  8. x. Test name
  9. Ticks: xxxxx //Empty String
  10. Ticks: xxxxx //two space
  11. Ticks: xxxxx //single letter
  12. Ticks: xxxxx //single letter with space
  13. Ticks: xxxxx //long string
  14. Ticks: xxxxx //long string with space
  15.  
  16. 1. if (myString.Trim().Length == 0)
  17. ticks: 4121800
  18. ticks: 7523992
  19. ticks: 17655496
  20. ticks: 29312608
  21. ticks: 17302880
  22. ticks: 38160224
  23.  
  24. 2. if (myString.Trim() == "")
  25. ticks: 4862312
  26. ticks: 8436560
  27. ticks: 21833776
  28. ticks: 32822200
  29. ticks: 21655224
  30. ticks: 42358016
  31.  
  32.  
  33. 3. if (myString.Trim().Equals(""))
  34. ticks: 5358744
  35. ticks: 9336728
  36. ticks: 18807512
  37. ticks: 30340392
  38. ticks: 18598608
  39. ticks: 39978008
  40.  
  41.  
  42. 4. if (myString.Trim() == String.Empty)
  43. ticks: 4848368
  44. ticks: 8306312
  45. ticks: 21552736
  46. ticks: 32081168
  47. ticks: 21486048
  48. ticks: 41667608
  49.  
  50.  
  51. 5. if (myString.Trim().Equals(String.Empty))
  52. ticks: 5372720
  53. ticks: 9263696
  54. ticks: 18677728
  55. ticks: 29634320
  56. ticks: 18551904
  57. ticks: 40183768
  58.  
  59.  
  60. 6. if (IsEmptyOrWhitespace(myString)) //See John Skeet's Post for algorithm
  61. ticks: 6597776
  62. ticks: 9988304
  63. ticks: 7855664
  64. ticks: 7826296
  65. ticks: 7885200
  66. ticks: 7872776
  67.  
  68. 7. is (string.IsNullOrEmpty(myString.Trim()) //Cloud's suggestion
  69. ticks: 4302232
  70. ticks: 10200344
  71. ticks: 18425416
  72. ticks: 29490544
  73. ticks: 17800136
  74. ticks: 38161368
  75.  
  76. public void Main()
  77. {
  78.  
  79. string res = string.Empty;
  80.  
  81. for (int j = 0; j <= 5; j++) {
  82.  
  83. string myString = "";
  84.  
  85. switch (j) {
  86.  
  87. case 0:
  88. myString = "";
  89. break;
  90. case 1:
  91. myString = " ";
  92. break;
  93. case 2:
  94. myString = "x";
  95. break;
  96. case 3:
  97. myString = "x ";
  98. break;
  99. case 4:
  100. myString = "this is a long string for testing triming empty things.";
  101. break;
  102. case 5:
  103. myString = "this is a long string for testing triming empty things. ";
  104.  
  105. break;
  106. }
  107.  
  108. bool result = false;
  109. Stopwatch sw = new Stopwatch();
  110.  
  111. sw.Start();
  112. for (int i = 0; i <= 100000; i++) {
  113.  
  114.  
  115. result = myString.Trim().Length == 0;
  116. }
  117. sw.Stop();
  118.  
  119.  
  120. res += "ticks: " + sw.ElapsedTicks + Environment.NewLine;
  121. }
  122.  
  123.  
  124. Console.ReadKey(); //break point here to get the results
  125. }
  126.  
  127. public static bool IsEmptyOrWhitespace(string text)
  128. {
  129. // Avoid creating iterator for trivial case
  130. if (text.Length == 0)
  131. {
  132. return true;
  133. }
  134. foreach (char c in text)
  135. {
  136. // Could use Char.IsWhiteSpace(c) instead
  137. if (c==' ' || c=='t' || c=='r' || c=='n')
  138. {
  139. continue;
  140. }
  141. return false;
  142. }
  143. return true;
  144. }
  145.  
  146. for (int i = 0; i < text.Length; i++)
  147. {
  148. char c = text[i];
  149. // ...
  150.  
  151. int length = text.Length;
  152. for (int i = 0; i < length; i++)
  153. {
  154. char c = text[i];
  155.  
  156. if (c != ' ' && c != 't' && c != 'r' && c != 'n')
  157. {
  158. return false;
  159. }
  160.  
  161. switch (c)
  162. {
  163. case ' ': case 'r': case 'n': case 't':
  164. return false;
  165. }
  166.  
  167. using System;
  168.  
  169. class Test
  170. {
  171. static void Main()
  172. {
  173. CheckTrim(string.Copy(""));
  174. CheckTrim(" ");
  175. CheckTrim(" x ");
  176. CheckTrim("xx");
  177. }
  178.  
  179. static void CheckTrim(string text)
  180. {
  181. string trimmed = text.Trim();
  182. Console.WriteLine ("Text: '{0}'", text);
  183. Console.WriteLine ("Trimmed ref == text? {0}",
  184. object.ReferenceEquals(text, trimmed));
  185. Console.WriteLine ("Trimmed ref == ""? {0}",
  186. object.ReferenceEquals("", trimmed));
  187. Console.WriteLine();
  188. }
  189. }
  190.  
  191. using System;
  192. using BenchmarkHelper;
  193.  
  194. public class TrimStrings
  195. {
  196. static void Main()
  197. {
  198. Test("");
  199. Test(" ");
  200. Test(" x ");
  201. Test("x");
  202. Test(new string('x', 1000));
  203. Test(" " + new string('x', 1000) + " ");
  204. Test(new string(' ', 1000));
  205. }
  206.  
  207. static void Test(string text)
  208. {
  209. bool expectedResult = text.Trim().Length == 0;
  210. string title = string.Format("Length={0}, result={1}", text.Length,
  211. expectedResult);
  212.  
  213. var results = TestSuite.Create(title, text, expectedResult)
  214. /* .Add(x => x.Trim().Length == 0, "Trim().Length == 0")
  215. .Add(x => x.Trim() == "", "Trim() == """)
  216. .Add(x => x.Trim().Equals(""), "Trim().Equals("")")
  217. .Add(x => x.Trim() == string.Empty, "Trim() == string.Empty")
  218. .Add(x => x.Trim().Equals(string.Empty), "Trim().Equals(string.Empty)")
  219. */
  220. .Add(OriginalIsEmptyOrWhitespace)
  221. .Add(IsEmptyOrWhitespaceForLoop)
  222. .Add(IsEmptyOrWhitespaceForLoopReversed)
  223. .Add(IsEmptyOrWhitespaceForLoopHoistedLength)
  224. .RunTests()
  225. .ScaleByBest(ScalingMode.VaryDuration);
  226.  
  227. results.Display(ResultColumns.NameAndDuration | ResultColumns.Score,
  228. results.FindBest());
  229. }
  230.  
  231. public static bool OriginalIsEmptyOrWhitespace(string text)
  232. {
  233. if (text.Length == 0)
  234. {
  235. return true;
  236. }
  237. foreach (char c in text)
  238. {
  239. if (c==' ' || c=='t' || c=='r' || c=='n')
  240. {
  241. continue;
  242. }
  243. return false;
  244. }
  245. return true;
  246. }
  247.  
  248. public static bool IsEmptyOrWhitespaceForLoop(string text)
  249. {
  250. for (int i=0; i < text.Length; i++)
  251. {
  252. char c = text[i];
  253. if (c==' ' || c=='t' || c=='r' || c=='n')
  254. {
  255. continue;
  256. }
  257. return false;
  258. }
  259. return true;
  260. }
  261.  
  262. public static bool IsEmptyOrWhitespaceForLoopReversed(string text)
  263. {
  264. for (int i=text.Length-1; i >= 0; i--)
  265. {
  266. char c = text[i];
  267. if (c==' ' || c=='t' || c=='r' || c=='n')
  268. {
  269. continue;
  270. }
  271. return false;
  272. }
  273. return true;
  274. }
  275.  
  276. public static bool IsEmptyOrWhitespaceForLoopHoistedLength(string text)
  277. {
  278. int length = text.Length;
  279. for (int i=0; i < length; i++)
  280. {
  281. char c = text[i];
  282. if (c==' ' || c=='t' || c=='r' || c=='n')
  283. {
  284. continue;
  285. }
  286. return false;
  287. }
  288. return true;
  289. }
  290. }
  291.  
  292. ============ Length=0, result=True ============
  293. OriginalIsEmptyOrWhitespace 30.012 1.00
  294. IsEmptyOrWhitespaceForLoop 30.802 1.03
  295. IsEmptyOrWhitespaceForLoopReversed 32.944 1.10
  296. IsEmptyOrWhitespaceForLoopHoistedLength 35.113 1.17
  297.  
  298. ============ Length=1, result=True ============
  299. OriginalIsEmptyOrWhitespace 31.150 1.04
  300. IsEmptyOrWhitespaceForLoop 30.051 1.00
  301. IsEmptyOrWhitespaceForLoopReversed 31.602 1.05
  302. IsEmptyOrWhitespaceForLoopHoistedLength 33.383 1.11
  303.  
  304. ============ Length=3, result=False ============
  305. OriginalIsEmptyOrWhitespace 30.221 1.00
  306. IsEmptyOrWhitespaceForLoop 30.131 1.00
  307. IsEmptyOrWhitespaceForLoopReversed 34.502 1.15
  308. IsEmptyOrWhitespaceForLoopHoistedLength 35.690 1.18
  309.  
  310. ============ Length=1, result=False ============
  311. OriginalIsEmptyOrWhitespace 31.626 1.05
  312. IsEmptyOrWhitespaceForLoop 30.005 1.00
  313. IsEmptyOrWhitespaceForLoopReversed 32.383 1.08
  314. IsEmptyOrWhitespaceForLoopHoistedLength 33.666 1.12
  315.  
  316. ============ Length=1000, result=False ============
  317. OriginalIsEmptyOrWhitespace 30.177 1.00
  318. IsEmptyOrWhitespaceForLoop 33.207 1.10
  319. IsEmptyOrWhitespaceForLoopReversed 30.867 1.02
  320. IsEmptyOrWhitespaceForLoopHoistedLength 31.837 1.06
  321.  
  322. ============ Length=1002, result=False ============
  323. OriginalIsEmptyOrWhitespace 30.217 1.01
  324. IsEmptyOrWhitespaceForLoop 30.026 1.00
  325. IsEmptyOrWhitespaceForLoopReversed 34.162 1.14
  326. IsEmptyOrWhitespaceForLoopHoistedLength 34.860 1.16
  327.  
  328. ============ Length=1000, result=True ============
  329. OriginalIsEmptyOrWhitespace 30.303 1.01
  330. IsEmptyOrWhitespaceForLoop 30.018 1.00
  331. IsEmptyOrWhitespaceForLoopReversed 35.475 1.18
  332. IsEmptyOrWhitespaceForLoopHoistedLength 40.927 1.36
  333.  
  334. if (String.IsNullOrEmpty(myString.Trim()))
  335.  
  336. if (myString.Trim().Length == 0)
  337.  
  338. if (String.IsNullOrEmpty(myString.Trim()))
  339.  
  340. if (String.IsNullOrEmpty(myString))
  341. {
  342. myString.Trim;
  343. //do the rest of you code
  344. }
  345.  
  346. public static bool IsNullOrEmpty(this String str, bool checkTrimmed)
  347. {
  348. var b = String.IsNullOrEmpty(str);
  349. return checkTrimmed ? b && str.Trim().Length == 0 : b;
  350. }
Add Comment
Please, Sign In to add comment