C#输出参数性能 [英] C# out parameter performance
问题描述
待办事项的退出在参数 C#的任何性能影响我应该知道的? (像例外)
Do out parameters in C# have any performance implications I should know about? (Like exceptions)
我的意思是,它有在一个退出
参数的方法是个好主意循环运行一对夫妇的第二个百万次?
I mean, is it a good idea to have a method with an out
parameter in a loop that will run a couple of million times a second?
我知道这是丑陋的,但我现在用同样的方式为 Int32.TryParse
使用它们 - 返回布尔
来判断一些验证是成功的并且具有退出
包含一些额外的数据,如果它是成功的参数。
I know it's ugly but I am using it the same way as Int32.TryParse
is using them - returning a bool
to tell if some validation was successful and having an out
parameter containing some additional data if it was successful.
推荐答案
我怀疑你会发现任何显著性能损失为使用退出
参数。你一定要得到的信息返回给调用者以某种方式或其他 - 退出
只是做不同的方式。您可能会发现还有的部分的罚款,如果您使用了大量参数的方法中,因为这可能意味着重定向每个访问额外的级别。不过,我不希望它是显著。作为正常的,写最可读的代码和测试中的表现是否已经。不够好试图进一步优化之前
I doubt that you'll find any significant performance penalty to using an out
parameter. You've got to get information back to the caller somehow or other - out
is just a different way of doing it. You may find there's some penalty if you use the out parameter extensively within the method, as it may well mean an extra level of redirection for each access. However, I wouldn't expect it to be significant. As normal, write the most readable code and test whether performance is already good enough before trying to optimise further.
编辑:这剩下的就是题外话,有效。这只是对于大值类型,通常应避免的真正相关反正:)
The rest of this is an aside, effectively. It's only really relevant for large value types, which should usually be avoided anyway :)
我与康拉德的说法不同意返回值的所有类型> 32位的处理方式类似或等同于出在机器的水平呢,尽管争论。这里有一个小测试程序:
I disagree with Konrad's assertion about "return values for all types > 32 bit are handled similar or identical to out arguments on the machine level anyway" though. Here's a little test app:
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
struct BigStruct
{
public Guid guid1, guid2, guid3, guid4;
public decimal dec1, dec2, dec3, dec4;
}
class Test
{
const int Iterations = 100000000;
static void Main()
{
decimal total = 0m;
// JIT first
ReturnValue();
BigStruct tmp;
OutParameter(out tmp);
Stopwatch sw = Stopwatch.StartNew();
for (int i=0; i < Iterations; i++)
{
BigStruct bs = ReturnValue();
total += bs.dec1;
}
sw.Stop();
Console.WriteLine("Using return value: {0}",
sw.ElapsedMilliseconds);
sw = Stopwatch.StartNew();
for (int i=0; i < Iterations; i++)
{
BigStruct bs;
OutParameter(out bs);
total += bs.dec1;
}
Console.WriteLine("Using out parameter: {0}",
sw.ElapsedMilliseconds);
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static BigStruct ReturnValue()
{
return new BigStruct();
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static void OutParameter(out BigStruct x)
{
x = new BigStruct();
}
}
结果:
Results:
Using return value: 11316
Using out parameter: 7461
基本上通过我们直接写入数据到最终目的地,而不是将其写入到小法的堆栈帧,然后将其复制到主方法的堆栈帧的输出参数。
Basically by using an out parameter we're writing the data directly to the final destination, rather than writing it to the small method's stack frame and then copying it back into the Main method's stack frame.
随意批评,虽然基准应用 - 我可能错过了一些东西。
Feel free to criticise the benchmark app though - I may have missed something!
这篇关于C#输出参数性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!