我对dynamicC#的性能有疑问.我读过dynamic让编译器再次运行,但是它做了什么?
是否必须dynamic使用用作参数的变量或仅具有动态行为/上下文的行重新编译整个方法?
我注意到使用dynamic变量可以将简单的for循环减慢2个数量级.
我玩过的代码:
internal class Sum2
{
public int intSum;
}
internal class Sum
{
public dynamic DynSum;
public int intSum;
}
class Program
{
private const int ITERATIONS = 1000000;
static void Main(string[] args)
{
var stopwatch = new Stopwatch();
dynamic param = new Object();
DynamicSum(stopwatch);
SumInt(stopwatch);
SumInt(stopwatch, param);
Sum(stopwatch);
DynamicSum(stopwatch);
SumInt(stopwatch);
SumInt(stopwatch, param);
Sum(stopwatch);
Console.ReadKey();
}
private static void Sum(Stopwatch stopwatch)
{
var sum = 0;
stopwatch.Reset();
stopwatch.Start();
for …Run Code Online (Sandbox Code Playgroud)