我正在使用 DynamicObject 的自定义实现,它非常适合我的应用程序,除了我遇到了一些性能问题。动态会产生一些性能开销,但我看到即使使用 ExpandoObject 也会有显着的(阅读:数量级)性能损失。
我不能使用 ExpandoObject 的原因是我想覆盖它的一些行为。我将问题归结为下面的一个非常简单的示例。
我的自定义 ExpandoObject 代码如下(简化为足以显示问题的代码)-
public class SuperExpando : DynamicObject
{
public Dictionary<string, object> dictionary = new Dictionary<string, object>();
public override bool TrySetMember(SetMemberBinder binder, object value)
{
dictionary[binder.Name] = value;
return true;
}
}
public dynamic m = new SuperExpando();
当我直接在 DynamicObject 的字典中设置值时(iemdictionary["keyname"] = 500),然后我看到类似于 ExpandoObject 的性能,即在亚毫秒时间内设置字典中键的值。当我使用 TrySetMember 覆盖 (iemkeyname = 500) 时,我看到每个键值集的性能下降到 30 毫秒 - 50 毫秒。当写入很多键时,这显然会成为一个问题。即使我一遍又一遍地写入同一个密钥,通过 TrySetMember 访问它也需要相同的时间。
我真正的性能问题似乎与我使用动态的事实无关,因为它使用 TrySetMember 覆盖。对于踢,我什至评论了
dictionary[binder.Name] = value;
在 TrySetMember 方法中,只留下一个“return true;”,性能是一样的。
如果我在我的 SuperExpando 类中添加如下内容——
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
if (dictionary.ContainsKey(binder.Name))
{
result = dictionary[binder.Name];
return true;
}
return false;
}
通过 TryGetMember 访问(读取)变量的性能问题是相同的,而直接读取字典提供了合理的性能。
有任何想法吗?
-BJ奎因
编辑:这是完整的示例代码。只需创建一个表单并在其上放置一个运行 go_Click 事件的按钮并将您的项目设置为控制台应用程序。对我来说,在 ExpandoObject 中设置所有 50 个键需要大约 30 毫秒,而 SuperExpando 至少需要大约 750 毫秒。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Dynamic;
namespace test
{
public partial class ExpandoTest : Form
{
public ExpandoTest()
{
InitializeComponent();
}
public class SuperExpando : DynamicObject
{
public Dictionary<string, object> dictionary = new Dictionary<string, object>();
public override bool TrySetMember(SetMemberBinder binder, object value)
{
//dictionary[binder.Name] = value;
return true;
}
}
DateTime lasttime = DateTime.Now;
public void outputtime(string label = "")
{
TimeSpan elapsedtime = DateTime.Now - lasttime;
Double elapsedms = elapsedtime.TotalMilliseconds;
Console.WriteLine(label + " : " + elapsedms.ToString());
lasttime = DateTime.Now;
}
private void go_Click(object sender, EventArgs e)
{
outputtime("Time spent waiting on user");
dynamic se = new SuperExpando();
outputtime("Declared SuperExpando");
se.test120 = 5;
se.test121 = 5;
se.test122 = 5;
se.test123 = 5;
se.test124 = 5;
se.test125 = 5;
se.test126 = 5;
se.test127 = 5;
se.test128 = 5;
se.test129 = 5;
se.test130 = 5;
se.test131 = 5;
se.test132 = 5;
se.test133 = 5;
se.test134 = 5;
se.test135 = 5;
se.test136 = 5;
se.test137 = 5;
se.test138 = 5;
se.test139 = 5;
se.test140 = 5;
se.test141 = 5;
se.test142 = 5;
se.test143 = 5;
se.test144 = 5;
se.test145 = 5;
se.test146 = 5;
se.test147 = 5;
se.test148 = 5;
se.test149 = 5;
se.test150 = 5;
se.test151 = 5;
se.test152 = 5;
se.test153 = 5;
se.test154 = 5;
se.test155 = 5;
se.test156 = 5;
se.test157 = 5;
se.test158 = 5;
se.test159 = 5;
se.test160 = 5;
se.test161 = 5;
se.test162 = 5;
se.test163 = 5;
se.test164 = 5;
se.test165 = 5;
se.test166 = 5;
se.test167 = 5;
se.test168 = 5;
se.test169 = 5;
outputtime("Time to Run SuperExpando, set 50 test key/value pairs -- (not even setting values, just returning true from TrySetMember!)");
dynamic eo = new ExpandoObject();
outputtime("Declared ExpandoObject");
eo.test120 = 5;
eo.test121 = 5;
eo.test122 = 5;
eo.test123 = 5;
eo.test124 = 5;
eo.test125 = 5;
eo.test126 = 5;
eo.test127 = 5;
eo.test128 = 5;
eo.test129 = 5;
eo.test130 = 5;
eo.test131 = 5;
eo.test132 = 5;
eo.test133 = 5;
eo.test134 = 5;
eo.test135 = 5;
eo.test136 = 5;
eo.test137 = 5;
eo.test138 = 5;
eo.test139 = 5;
eo.test140 = 5;
eo.test141 = 5;
eo.test142 = 5;
eo.test143 = 5;
eo.test144 = 5;
eo.test145 = 5;
eo.test146 = 5;
eo.test147 = 5;
eo.test148 = 5;
eo.test149 = 5;
eo.test150 = 5;
eo.test151 = 5;
eo.test152 = 5;
eo.test153 = 5;
eo.test154 = 5;
eo.test155 = 5;
eo.test156 = 5;
eo.test157 = 5;
eo.test158 = 5;
eo.test159 = 5;
eo.test160 = 5;
eo.test161 = 5;
eo.test162 = 5;
eo.test163 = 5;
eo.test164 = 5;
eo.test165 = 5;
eo.test166 = 5;
eo.test167 = 5;
eo.test168 = 5;
eo.test169 = 5;
outputtime("Time to Run ExpandoObject, set 50 test key/value pairs");
}
}
}