方法调用开销 [英] Method call overhead

查看:106
本文介绍了方法调用开销的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道什么是调用.NET中华征信于方法返回inmediatly(因为其中一个条件是第一个code内不符合)的开销。

I'd like to know what's the overhead of calling a method- in .Net -which returns inmediatly (because a condition which is the first code inside is not met).

我认为,无论怎样的实时应用程序是,开销可以忽略不计,并且在任何情况下,分析应该显示的事实,是可读性更重要的,但有些同事不同意我说你应该总是避免这些电话......

I'd argue that no matter how real-time your application is, that overhead is negligible and that in any case profiling should show the facts, being readability more important, but some co-workers do not agree with me and say that you should always avoid those calls...

任何其他的意见?

推荐答案

我敢说有一些优势情况下,它的可以的是显著 - 但他们会察觉罕见。我同意你的看法:第一次写的可读性

I dare say there are some edge cases where it could be significant - but they'll be vanishingly rare. I agree with you: write for readability first.

请注意,如果该方法是极短,JIT编译器的可以的内联它 - 从快可能,但如果它是一个大的方法,你刚才的发生的退出,它惯于。在极端情况下,可能要一分为二的方法:一短方法(可内联),以测试该方法的其他部分是否有效,以及一个真正做的工作。然后,您可以先进行测试,然后调用第二个方法。坦白说,我不喜欢它了,并会的只有的建议做它后,你会发现,这确实是一个问题......但至少你有一个建议,你的同事的可能性在哪里它确实已被证明是一个问题:)

Note that if the method is extremely short, the JIT compiler may inline it - but if it's a large method which you just happen to exit from quickly, it probably won't. In extreme cases, you may want to split the method in two: one short method (which can be inlined) to test whether the rest of the method is valid, and one to actually do the work. You could then perform the test first, then call the second method. Frankly I don't like it much, and would only suggest doing it after you'd discovered that this really was a problem... but at least you have a proposal for your colleagues for the eventuality where it really has been shown to be a problem :)

有一件事情你的可以的要考虑为理由,以避免方法调用是,如果评估的参数需要很长的时间。例如,考虑日志:

One thing you may want to think about as a reason to avoid method calls is if evaluating the arguments takes a long time. For example, consider logging:

Log.Info("I did something: {0}", action.GenerateDescription());

现在,如果 GenerateDescription 需要很长的时间,你不希望执行它,如果日志记录是不会反正要发生......所以,你可以写:

Now if GenerateDescription takes a long time, you don't want to execute it if logging isn't going to happen anyway... so you could write:

if (Log.IsEnabled(LogLevel.Info))
{
    Log.Info("I did something: {0}", action.GenerateDescription());
}

另一种方法是使用委托推迟的评价 - 尽管这可能有它自己的(小)成本:

Another alternative is to use a delegate to defer evaluation - although this may have its own (small) cost:

Log.Info("I did something: {0}", () => action.GenerateDescription());

或者使用方法gruop转换:

or using a method gruop conversion:

Log.Info("I did something: {0}", action.GenerateDescription);

这可能不是你的同事们担心的问题,但它是值得思考的:)

That may well not be the problem your colleagues are worrying about, but it's worth thinking about :)

这篇关于方法调用开销的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆