fork download
  1. using System;
  2. using System.Linq.Expressions;
  3. using System.Diagnostics;
  4. using System.Threading;
  5.  
  6. namespace ConsoleApplication
  7. {
  8. class Foo
  9. {
  10. public int Baz { get; set; }
  11.  
  12. }
  13.  
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. // Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
  19.  
  20. const int cycles = 10000;
  21.  
  22. dynamic lambda;
  23. Func<object, bool> lambda2;
  24. Delegate lambda3;
  25. Func<Foo, bool> lambda4;
  26.  
  27. Type theType = typeof(Foo);
  28. Expression right = Expression.Constant(1);
  29.  
  30. {
  31. ParameterExpression pe = Expression.Parameter(theType, "f");
  32. Expression left = Expression.Property(pe, theType.GetProperty("Baz"));
  33. Expression expr = Expression.Equal(left, right);
  34. // This works fine but uses a hard-coded type, which I won't know until runtime:
  35. lambda = Expression.Lambda(expr, new ParameterExpression[] { pe }).Compile();
  36. lambda3 = Expression.Lambda(expr, new ParameterExpression[] { pe }).Compile(); ;
  37. }
  38.  
  39. {
  40. ParameterExpression pe = Expression.Parameter(typeof(object), "f");
  41. Expression pe2 = Expression.Convert(pe, theType);
  42. Expression left = Expression.Property(pe2, theType.GetProperty("Baz"));
  43. Expression expr = Expression.Equal(left, right);
  44. lambda2 = Expression.Lambda<Func<object, bool>>(expr, new ParameterExpression[] { pe }).Compile();
  45. }
  46.  
  47. {
  48. ParameterExpression pe = Expression.Parameter(typeof(Foo), "f");
  49. Expression pe2 = Expression.Convert(pe, theType);
  50. Expression left = Expression.Property(pe2, theType.GetProperty("Baz"));
  51. Expression expr = Expression.Equal(left, right);
  52. lambda4 = Expression.Lambda<Func<Foo, bool>>(expr, new ParameterExpression[] { pe }).Compile();
  53. }
  54.  
  55. var t = new Foo { Baz = 1 };
  56.  
  57. for (int i = 0; i < 1000; i++)
  58. {
  59. lambda(t);
  60. lambda2(t);
  61. lambda3.DynamicInvoke(t);
  62. lambda4(t);
  63. }
  64.  
  65. Thread.Sleep(1000);
  66.  
  67. Stopwatch sw = Stopwatch.StartNew();
  68.  
  69. for (int i = 0; i < cycles; i++)
  70. {
  71. if (!lambda(t))
  72. {
  73. throw new Exception();
  74. }
  75. }
  76.  
  77. sw.Stop();
  78.  
  79. Stopwatch sw2 = Stopwatch.StartNew();
  80.  
  81. for (int i = 0; i < cycles; i++)
  82. {
  83. if (!lambda2(t))
  84. {
  85. throw new Exception();
  86. }
  87. }
  88.  
  89. sw2.Stop();
  90.  
  91. Stopwatch sw3 = Stopwatch.StartNew();
  92.  
  93. for (int i = 0; i < cycles; i++)
  94. {
  95. if (!(bool)lambda3.DynamicInvoke(t))
  96. {
  97. throw new Exception();
  98. }
  99. }
  100.  
  101. sw3.Stop();
  102.  
  103. Stopwatch sw4 = Stopwatch.StartNew();
  104.  
  105. for (int i = 0; i < cycles; i++)
  106. {
  107. if (!lambda4(t))
  108. {
  109. throw new Exception();
  110. }
  111. }
  112.  
  113. sw4.Stop();
  114.  
  115. Console.WriteLine("{0,12} dynamic", sw.ElapsedTicks);
  116. Console.WriteLine("{0,12} Func<object, bool>", sw2.ElapsedTicks);
  117. Console.WriteLine("{0,12} Delegate.DynamicInvoke", sw3.ElapsedTicks);
  118. Console.WriteLine("{0,12} Func<Foo, bool>", sw4.ElapsedTicks);
  119. }
  120. }
  121. }
  122.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(22,13): error CS1980: Dynamic keyword requires `System.Runtime.CompilerServices.DynamicAttribute' to be defined. Are you missing System.Core.dll assembly reference?
prog.cs(59,17): error CS0518: The predefined type `Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported
prog.cs(59,17): error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported
prog.cs(59,17): error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported
prog.cs(59,17): error CS1969: Dynamic operation cannot be compiled without `Microsoft.CSharp.dll' assembly reference
prog.cs(71,22): error CS0518: The predefined type `Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported
prog.cs(71,22): error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported
prog.cs(71,22): error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported
prog.cs(71,22): error CS1969: Dynamic operation cannot be compiled without `Microsoft.CSharp.dll' assembly reference
prog.cs(71,21): error CS0518: The predefined type `Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported
prog.cs(71,21): error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported
prog.cs(71,21): error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported
prog.cs(71,21): error CS1969: Dynamic operation cannot be compiled without `Microsoft.CSharp.dll' assembly reference
prog.cs(71,21): error CS0518: The predefined type `Microsoft.CSharp.RuntimeBinder.Binder' is not defined or imported
prog.cs(71,21): error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported
prog.cs(71,21): error CS0518: The predefined type `System.Runtime.CompilerServices.CallSite' is not defined or imported
prog.cs(71,21): error CS1969: Dynamic operation cannot be compiled without `Microsoft.CSharp.dll' assembly reference
Compilation failed: 17 error(s), 0 warnings
stdout
Standard output is empty