code_junkie

Which one of these code samples has better performance?

Nov 13th, 2011
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. while (!String.IsNullOrEmpty(msg = reader.readMsg()))
  2. {
  3. RaiseMessageReceived();
  4. if (parseMsg)
  5. {
  6. ParsedMsg parsedMsg = parser.parseMsg(msg);
  7. RaiseMessageParsed();
  8. if (processMsg)
  9. {
  10. process(parsedMsg);
  11. RaiseMessageProcessed();
  12. }
  13. }
  14. }
  15.  
  16. while (!String.IsNullOrEmpty(msg = reader.readMsg()))
  17. {
  18. RaiseMessageReceived();
  19. if (!parseMsg) continue;
  20.  
  21. ParsedMsg parsedMsg = parser.parseMsg(msg);
  22. RaiseMessageParsed();
  23. if (!processMsg) continue;
  24.  
  25. process(parsedMsg);
  26. RaiseMessageProcessed();
  27. }
  28.  
  29. using System;
  30. using System.Collections.Generic;
  31.  
  32. namespace ConsoleApplication2
  33. {
  34. class Program
  35. {
  36. static void Main(string[] args)
  37. {
  38. Tester t=new Tester();
  39. t.Method1(new Stack<string>(), new MsgParser(), true, true);
  40. t.Method2(new Stack<string>(), new MsgParser(), true, true);
  41. }
  42. }
  43. class Tester
  44. {
  45. public void Method1(Stack<string> strings, MsgParser parser, bool parseMsg, bool processMsg)
  46. {
  47. string msg;
  48. while (!String.IsNullOrEmpty(msg = strings.Pop()))
  49. {
  50. RaiseMessageReceived();
  51. if (parseMsg)
  52. {
  53. ParsedMsg parsedMsg = parser.ParseMsg(msg);
  54. RaiseMessageParsed();
  55. if (processMsg)
  56. {
  57. process(parsedMsg);
  58. RaiseMessageProcessed();
  59. }
  60. }
  61. }
  62. }
  63.  
  64. public void Method2(Stack<string> strings, MsgParser parser, bool parseMsg, bool processMsg)
  65. {
  66. string msg;
  67. while (!String.IsNullOrEmpty(msg = strings.Pop()))
  68. {
  69. RaiseMessageReceived();
  70. if (!parseMsg) continue;
  71.  
  72. ParsedMsg parsedMsg = parser.ParseMsg(msg);
  73. RaiseMessageParsed();
  74. if (!processMsg) continue;
  75.  
  76. process(parsedMsg);
  77. RaiseMessageProcessed();
  78. }
  79.  
  80. }
  81.  
  82. private void RaiseMessageProcessed()
  83. {
  84. Console.WriteLine("Done");
  85. }
  86.  
  87. private void process(ParsedMsg msg)
  88. {
  89. Console.WriteLine(msg);
  90. }
  91.  
  92. private void RaiseMessageParsed()
  93. {
  94. Console.WriteLine("Message parsed");
  95. }
  96.  
  97. private void RaiseMessageReceived()
  98. {
  99. Console.WriteLine("Message received.");
  100. }
  101. }
  102.  
  103. internal class ParsedMsg
  104. {
  105. }
  106.  
  107. internal class MsgParser
  108. {
  109. public ParsedMsg ParseMsg(string msg)
  110. {
  111. return new ParsedMsg();
  112. }
  113. }
  114. }
  115.  
  116. internal class Tester
  117. {
  118. // Methods
  119. public void Method1(Stack<string> strings, MsgParser parser, bool parseMsg, bool processMsg)
  120. {
  121. string msg;
  122. while (!string.IsNullOrEmpty(msg = strings.Pop()))
  123. {
  124. this.RaiseMessageReceived();
  125. if (parseMsg)
  126. {
  127. ParsedMsg parsedMsg = parser.ParseMsg(msg);
  128. this.RaiseMessageParsed();
  129. if (processMsg)
  130. {
  131. this.process(parsedMsg);
  132. this.RaiseMessageProcessed();
  133. }
  134. }
  135. }
  136. }
  137.  
  138. public void Method2(Stack<string> strings, MsgParser parser, bool parseMsg, bool processMsg)
  139. {
  140. string msg;
  141. while (!string.IsNullOrEmpty(msg = strings.Pop()))
  142. {
  143. this.RaiseMessageReceived();
  144. if (parseMsg)
  145. {
  146. ParsedMsg parsedMsg = parser.ParseMsg(msg);
  147. this.RaiseMessageParsed();
  148. if (processMsg)
  149. {
  150. this.process(parsedMsg);
  151. this.RaiseMessageProcessed();
  152. }
  153. }
  154. }
  155. }
  156.  
  157. private void process(ParsedMsg msg)
  158. {
  159. Console.WriteLine(msg);
  160. }
  161.  
  162. private void RaiseMessageParsed()
  163. {
  164. Console.WriteLine("Message parsed");
  165. }
  166.  
  167. private void RaiseMessageProcessed()
  168. {
  169. Console.WriteLine("Done");
  170. }
  171.  
  172. private void RaiseMessageReceived()
  173. {
  174. Console.WriteLine("Message received.");
  175. }
  176. }
Add Comment
Please, Sign In to add comment