code_junkie

How much computation is behind a HttpContext.Current call?

Nov 12th, 2011
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. <% Validator.RenderClient(Response.Output); %>
  2.  
  3. <% Validator.RenderClient(); %>
  4.  
  5. var results1 = new List<long>();
  6. var results2 = new List<long>();
  7.  
  8. for (int j = 0; j < 100; j++)
  9. {
  10. var sp = new System.Diagnostics.Stopwatch();
  11.  
  12. // With HttpContext.Current:
  13. sp.Start();
  14. for (int i = 0; i < 10000; i++)
  15. {
  16. HttpContext.Current.Response.Output.Write(i);
  17. }
  18. sp.Stop();
  19.  
  20. results1.Add(sp.ElapsedTicks);
  21.  
  22. // Without:
  23. TextWriter output2 = HttpContext.Current.Response.Output;
  24. sp.Reset();
  25.  
  26. sp.Start();
  27. for (int i = 0; i < 10000; i++)
  28. {
  29. output2.Write(i);
  30. }
  31. sp.Stop();
  32.  
  33. HttpContext.Current.Response.Clear();
  34.  
  35. results2.Add(sp.ElapsedTicks);
  36. }
  37.  
  38. results1.Sort();
  39. results2.Sort();
  40.  
  41. HttpContext.Current.Response.Write(string.Format("HttpContext.Current={0:0.000}ms, Local variable={1:0.000}ms, R={2:0.0%}<br/>", results1[results1.Count / 2] / (double)TimeSpan.TicksPerMillisecond, results2[results2.Count / 2] / (double)TimeSpan.TicksPerMillisecond, (double)results1[results1.Count / 2] / (double)results2[results2.Count / 2]));
  42.  
  43. HttpContext.Current=0,536ms, Local variable=0,486ms, R=110,2%
  44.  
  45. public static HttpContext get_Current()
  46. {
  47. return (ContextBase.Current as HttpContext);
  48. }
  49.  
  50. public static object HostContext
  51. {
  52. get
  53. {
  54. object hostContext =
  55. Thread.CurrentThread.GetIllogicalCallContext().HostContext;
  56. if (hostContext == null)
  57. {
  58. hostContext = GetLogicalCallContext().HostContext;
  59. }
  60. return hostContext;
  61. }
  62.  
  63. System.Diagnostics.Stopwatch sp = new System.Diagnostics.Stopwatch();
  64.  
  65. // With HttpContext.Current:
  66. sp.Start();
  67. for (int i = 0; i < 100; i++)
  68. {
  69. HttpContext.Current.Response.Output.Write(i.ToString());
  70. }
  71. sp.Stop();
  72. long result1 = sp.ElapsedTicks;
  73.  
  74. // Without:
  75. TextWriter output2 = HttpContext.Current.Response.Output;
  76. sp.Reset();
  77. sp.Start();
  78. for (int i = 0; i < 100; i++)
  79. {
  80. output2.Write(i.ToString());
  81. }
  82. sp.Stop();
  83. long result2 = sp.ElapsedTicks;
Add Comment
Please, Sign In to add comment