简体   繁体   English

执行时间有差异吗?

[英]Execution time differences, are there any?

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 ;)) 现在我可以走的另一条路是,我有一个类(AppManager),大多数成员都是静态的,(从旧版代码中,不建议我单身;))


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? 哪种形式更清洁?

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. ABI会将“ this”指针放入一个寄存器(或堆栈)中,而这在静态函数中是不会执行的,但是最终结果几乎是不可测量的。

Bottom line - write your code in the cleanest possible way. 底线-以最简洁的方式编写代码。 Performance is not a concern at this point. 此时,性能不是问题。

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. 但是,当您调用静态函数时,它只是一个函数调用,它比内联函数具有更多的开销。

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. 在大多数情况下,如果您的代码结构合理,则很难获得任何形式的优化来提高速度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 有什么方法可以减少此代码的执行时间? - Is there any way to reduce execution time of this code? 几乎相同的C ++和Python代码的执行时间差异非常大 - Very large execution time differences for virtually same C++ and Python code C / C ++是否能在最短的执行时间内提供任何保证? - Does C/C++ offer any guarantee on minimal execution time? OpenMP:嵌套的for循环,执行时间几乎没有差异 - OpenMP: Nested for-loop, barely any difference in execution time 在运行时可以检测到C ++ 03和C ++ 11之间有什么区别? - What differences, if any, between C++03 and C++11 can be detected at run-time? 不同的Ubuntu版本二进制执行差异 - Different Ubuntu versions binary execution differences 这两行之间有什么不同吗? - Are there any differences between these two lines? `boost :: any`和`std :: any`之间的区别 - Differences between `boost::any` and `std::any` C ++代码执行时间随着源代码的变化而变化,不应引入任何额外的工作 - C++ code execution time varies with small source change that shouldn't introduce any extra work C++:是否可以在执行时设置void-pointer、variant-object 或any-object 的类型? - C++: Is it posible to set the type of a void-pointer, variant-object or any-object in execution time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM