执行时间有差异吗?

发布于 2024-10-05 02:35:38 字数 389 浏览 4 评论 0原文

考虑这段代码:


class A {
  void methodX() {
  // snip (1 liner function)
  }
}

class B {
  void methodX() {
   // same -code
  }
}
 

现在我可以采取的另一种方式是,我有一个类(AppManager),其大多数成员都是静态的,(来自遗留代码,不建议我单例;))


class AppManager {
  public:
  static void methodX(){
   // same-code
  }
}

应该首选哪一个? 由于两者都是内联的,因此不应该存在运行时差异,对吧?
哪种形式更干净?

Consider this piece of code:


class A {
  void methodX() {
  // snip (1 liner function)
  }
}

class B {
  void methodX() {
   // same -code
  }
}
 

Now other way i can go is, I have a class(AppManager) most of whose members are static, (from legacy code, don't suggest me singleton ;))


class AppManager {
  public:
  static void methodX(){
   // same-code
  }
}

Which one should be preferred?
As both are inlined, there shouldn't be a runtime difference, right?
Which form is more cleaner?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

泡沫很甜 2024-10-12 02:35:38

首先,这是一个很小的问题,除非每帧调用函数数千次(并且您正在做一些“帧”很重要的事情),否则您永远不必担心它。

其次,如果它们是内联的,代码将(希望)得到优化,以至于没有任何迹象表明该函数是非静态的。它会是相同的。

即使它们没有内联,差异也会很小。 ABI 会将“this”指针放入寄存器(或堆栈)中,这在静态函数中不会执行,但同样,最终结果几乎无法测量。

底线 - 以尽可能简洁的方式编写代码。此时性能不是问题。

Now first of all, this is a concern so minuscule that you would never have to worry about it unless the functions are called thousands of times per frame (and you're doing something where "frames" matter).

Second, IF they are inlined, the code will be (hopefully) optimized so much that there is no sign whatsoever of the function being non-static. It would be identical.

Even if they were not inlined, the difference would be minor. The ABI would put the "this" pointer into a register (or the stack), which it wouldn't do in a static function, but again, the net result would be almost not measurable.

Bottom line - write your code in the cleanest possible way. Performance is not a concern at this point.

勿挽旧人 2024-10-12 02:35:38

在我看来,内联方式会更快。
因为内联函数在编译时被代码替换,因此不需要保存寄存器、进行函数调用然后再次返回。但是当你调用静态函数时,它只是一个函数调用,并且比内联函数有更多的开销。

In my opinion Inline way would be faster.
because inline functions are replaced in code in compile time and therefor there is no need to save registers, make a function call and then return again. but when you call a static function it's just a function call and it has much overhead than the inline one.

第几種人 2024-10-12 02:35:38

我认为这是最常见的优化问题。在第一级,当你编写代码时,你会尝试每一个有助于编译器的技巧,所以如果编译器不能很好地优化代码,你已经有了。这是错误的。在编写代码的第一阶段优化中,您所寻找的只是干净且易于理解的代码、设计和结构。这将产生更好的代码,手动“优化”。

规则是:
如果您没有资源来对代码进行基准测试,请重写它并花费大量时间进行优化,而不是不需要优化代码。在大多数情况下,如果您的代码结构良好,则很难通过任何类型的优化来获得任何速度提升。

I think that this is most common optimisation problem. At first level when you writing a code you try every single trick that would help compiler so if compiler can not optimise code well, you already have. This is wrong. What are you looking for in first stage of optimisation during writing code is just clean and understandable code, design and structure. That will make by far better code, that "optimised" by hand.

Rule is:
If you do not have resources to benchmark code, rewrite it and spend lot of time for optimisation than you do not need optimised code. In most cases it is hard to gain any speed boost whit any kind optimisation, if you structured your code well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文