以下每个解决方案我都运行了131072(128 * 1024)次迭代(在一个单独的线程中)。
- 只读
MethodInvoker
:5664.53(+0%)
- 新的
MethodInvoker
:5828.31(+2.89%)
- 在
MethodInvoker
中进行函数转换:5857.07 (+3.40%)
- 只读
Action
:6467.33(+14.17%)
- 新的
Action
:6829.07(+20.56%)
在每次迭代中调用一个新的 Action
private void SetVisibleByNewAction()
{
if (InvokeRequired)
{
Invoke(new Action(SetVisibleByNewAction));
}
else
{
Visible = true;
}
}
每次迭代调用只读内置构造函数Action
private void SetVisibleByAction()
{
if (InvokeRequired)
{
Invoke(_actionSetVisibleByAction);
}
else
{
Visible = true;
}
}
在每次迭代中调用一个新的MethodInvoker。
private void SetVisibleByNewMethodInvoker()
{
if (InvokeRequired)
{
Invoke(new MethodInvoker(SetVisibleByNewMethodInvoker));
}
else
{
Visible = true;
}
}
在每次迭代中调用只读、内置构造函数MethodInvoker
// private readonly MethodInvoker _methodInvokerSetVisibleByMethodInvoker
// _methodInvokerSetVisibleByMethodInvoker = SetVisibleByMethodInvoker;
private void SetVisibleByMethodInvoker()
{
if (InvokeRequired)
{
Invoke(_methodInvokerSetVisibleByMethodInvoker);
}
else
{
Visible = true;
}
}
在每次迭代中调用MethodInvoker函数进行转换
private void SetVisibleByDelegate()
{
if (InvokeRequired)
{
Invoke((MethodInvoker) SetVisibleByDelegate);
}
else
{
Visible = true;
}
}
呼叫“新行动”解决方案的示例:
private void ButtonNewActionOnClick(object sender, EventArgs e)
{
new Thread(TestNewAction).Start();
}
private void TestNewAction()
{
var watch = Stopwatch.StartNew();
for (var i = 0; i < COUNT; i++)
{
SetVisibleByNewAction();
}
watch.Stop();
Append("New Action: " + watch.ElapsedMilliseconds + "ms");
}