多长时间需要调用一个空函数? [英] How long does it take to invoke an empty function?

查看:136
本文介绍了多长时间需要调用一个空函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有实现一个接口的项目的列表。对于这个问题,让我们用这个例子接口:

 接口人物
{
无效AgeAYear();
}

有两个班

 类NormalPerson:消费
{
INT年龄= 0;

无效AgeAYear()
{
++岁;
//做一些更多的东西...
}
}


类ImmortalPerson:消费
{
无效AgeAYear ()
{
//什么也不做......
}
}

有关其他的原因,我需要的名单他们俩。但对于这一呼吁,当我遍历我的列表,我可能会调用空函数。 这会不会影响性能?如果是的话,多少钱?将空的功能,所有意图和目的,进行优化呢?






注意:在实际例子中, ImmortalPerson 有其他方法确实有代码 - 它不只是一个对象,它什么都不做。


< DIV CLASS =h2_lin>解决方案

这会不会影响性能?




极不可能有有意义的性能影响。




如果是这样,多少钱?




您可以量化它的究竟作为使用分析特定的代码路径。我们不能,因为我们不知道的代码路径。我们可以猜测,并告诉你它几乎肯定不要紧,因为这是极不可能在你的应用程序中的瓶颈(你真的坐在那里打电话 Person.AgeAYear 中一个紧密的循环?)。



只有你可以找出恰恰按失控的探查和测量。



<块引用>

请问空函数,对于所有意图和目的,进行优化呢?




这当然是可能的,但它可能不是;它甚至可能在抖动的未来版本中改变,或者从平台切换到平台(不同的平台有不同的JITters)。如果你真的想知道,编译应用程序,并期待在反汇编代码即时编译(!不是IL)



但我会这样说:这几乎是当然,几乎可以肯定不是什么值得担心或将随时进入。除非你调用 Person.AgeAYear 在性能关键代码一个紧密的循环,它不是在你的应用的瓶颈。你可以花的这个时候,或者你可以花时间改善你的应用程序。你的时间有机会成本太高。


I have a list of items implementing an interface. For the question, let's use this example interface:

interface Person
{
  void AgeAYear();
}

There are two classes

class NormalPerson : Person
{
  int age = 0;

  void AgeAYear()
  {
    age++;
    //do some more stuff...
  }
}


class ImmortalPerson : Person
{
  void AgeAYear()
  {
    //do nothing...
  }
}

For other reasons, I need them both of the list. But for this call, when I loop through my list of Persons, I may be calling empty functions. Will this have a performance impact? If so, how much? Will the empty function, for all intents and purposes, be optimized out?


NOTE: In the real example, the ImmortalPerson has other methods that do have code - it is not just an object that does nothing.

解决方案

Will this have a performance impact?

Highly unlikely to have a meaningful performance impact.

If so, how much?

You can quantify it exactly for your specific code paths using a profiler. We can not, because we don't know the code paths. We can guess, and tell you it almost surely doesn't matter because this is extremely unlikely to be a bottleneck in your application (are you really sitting there calling Person.AgeAYear in a tight loop?).

Only you can find out precisely by getting out a profiler and measuring.

Will the empty function, for all intents and purposes, be optimized out?

It's certainly possible but it might not; it might even change in future version of the JITter, or change from platform to platform (different platforms have different JITters). If you really want to know, compile your application, and look at the disassembled JITted code (not the IL!).

But I'll say this: this is almost surely, almost definitely not something worth worrying about or putting any time into. Unless you are calling Person.AgeAYear in a tight loop in performance critical code, it's not a bottleneck in your application. You could spend time on this, or you could spend time improving your application. Your time has an opportunity cost too.

这篇关于多长时间需要调用一个空函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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