C#比較dynamic和Dictionary效能

Sartrey Lee 的部落格發表於2015-03-12

開發中需要傳遞變參,考慮使用 dynamic 還是 Dictionary(準確地說是Dictionary<string,object>)。dynamic 的編碼體驗顯著優於 Dictionary,如果效能差距不大的話,我會選擇使用dynamic。搜尋後沒有找到類似對比資料,決定自行實驗。

首先使用以下測試程式碼:

public void TestDynamic()
{
    var e = CallDynamic(new { Value = 0 });
    int v = e.Value;
}

public void TestDictionary() 
{
    var dict = new Dictionary<string, object>();
    dict["Value"] = 0;
    dict = CallDictionary(dict);
    int v = (int)dict["Value"];
}

private dynamic CallDynamic(dynamic test)
{
    int v = test.Value;
    v++;
    return new { Value = v };
}

private Dictionary<string, object> CallDictionary(
    Dictionary<string, object> test)
{
    int v = (int)test["Value"];
    v++;
    var dict = new Dictionary<string, object>();
    dict["Value"] = v;
    return dict;
}

分別比較執行 1次、10次、100次、1000次、1e4次、1e5次、1e6次 時間
結果:

其中dynamic列和dynamic2列的資料分別是:

在一次執行中執行一步測試 和 在一次執行中連續執行所有測試

分析測試過程和資料,得到以下結論:

1.dynamic首次使用會產生一定的效能損耗
2.無論是否首次使用,使用次數達到一定量級,dynamic效能一定優於Dictionary
3.一次執行中連續使用dynamic會顯著拉低平均效能損耗

考慮到傳遞變參可能出現多個引數,以上測試不完全。

使用以下程式碼進行第二階段實驗:

public void InvokeDynamic()
{
    var e = CallDynamic2(
        new { Value1 = 0, Value2 = 0L, Value3 = 0f, Value4 = 0.0, Value5 = "test" });
    int v1 = e.Value1;
    long v2 = e.Value2;
    float v3 = e.Value3;
    double v4 = e.Value4;
    string v5 = e.Value5;
}

public void InvokeDictionary()
{
    var dict = new Dictionary<string, object>();
    dict["Value1"] = 0;
    dict["Value2"] = 0L;
    dict["Value3"] = 0f;
    dict["Value4"] = 0.0;
    dict["Value5"] = "test";
    dict = CallDictionary2(dict);
    int v1 = (int)dict["Value1"];
    long v2 = (long)dict["Value2"];
    float v3 = (float)dict["Value3"];
    double v4 = (double)dict["Value4"];
    string v5 = (string)dict["Value5"];
}

private dynamic CallDynamic2(dynamic test)
{
    int v1 = test.Value1;
    long v2 = test.Value2;
    float v3 = test.Value3;
    double v4 = test.Value4;
    string v5 = test.Value5;
    v1++;
    v2++;
    v3++;
    v4++;
    v5 += "test";
    return new { Value1 = v1, Value2 = v2, Value3 = v3, Value4 = v4, Value5 = v5 };
}

private Dictionary<string, object> CallDictionary2(
    Dictionary<string, object> test)
{
    int v1 = (int)test["Value1"];
    long v2 = (long)test["Value2"];
    float v3 = (float)test["Value3"];
    double v4 = (double)test["Value4"];
    string v5 = (string)test["Value5"];
    v1++;
    v2++;
    v3++;
    v4++;
    v5 += "test";
    var dict = new Dictionary<string, object>();
    dict["Value1"] = v1;
    dict["Value2"] = v2;
    dict["Value3"] = v3;
    dict["Value4"] = v4;
    dict["Value5"] = v5;
    return dict;
}

結果資料:

最後決定選擇使用dynamic

有兄弟考慮可能Box損耗了效能導致Dictionary表現不佳,
專門做了第三階段實驗,對比dynamic和Dictionary<string,long>

具體資料不貼了,結果是dynamic在100000量級快一倍

相關文章