C ++函数static local,performance? [英] C++ Function static local, performance?

查看:135
本文介绍了C ++函数static local,performance?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个必须返回true的函数,取决于表示运算符类型的成员枚举的值。

I have a function that must return true of false depending on the value of a member enum representing the type of operator.

我想知道什么是以下选项之间的最快速度,因为我不知道编译器将要做什么隐式优化(如果有)。

I'm wondering what would be the fastest between the following options, as I'm not sure of what implicit optimizations the compiler is going to do, if any.

   inline bool isBinaryOper( void ) const // Fastest i assume.
   {
      static const bool arr[] =
      {
         true,  // E_PLUS
         true,  // E_MINUS
         true,  // E_MULTIPLY
         true,  // E_DIVIDE
         false, // E_LPARENT
         false, // E_RPARENT
         false, // E_COMMA
         false  // E_SEMICOLON
      };

      return arr[ size_t( this->_eType ) ]; // Assuming values are valid indexes.
   }

或:

   inline bool isBinaryOper( void ) const
   {
      switch( this->_eType )
      {
         case E_PLUS      : return true;
         case E_MINUS     : return true;
         case E_MULTIPLY  : return true;
         case E_DIVIDE    : return true;
         case E_LPARENT   : return false;
         case E_RPARENT   : return false;
         case E_COMMA     : return false;
         case E_SEMICOLON : return false;
         default : ...
      };
   }

或者,我想是非常类似于前一个:

Or, which I guess is very similar to the previous one :

   inline bool isBinaryOper( void ) const
   {
      if      ( this->_eType == E_PLUS  ) return true;
      else if ( this->_eType == E_MINUS ) return true;
      // etc...
   }

哪一个会是最快的,为什么?

Which one would be the fastest, and why ?

推荐答案

这个问题引发我作为一个未成熟的优化实例,请使用开关语句,即使它可能稍微更慢,因为:

This question strikes me as being an instance of premature optimization, but for what its worth, I'd go with the switch statement even though it is likely to be slightly slower, because:


    <

假设您填写默认值:

Assuming you fill in the default: case, the switch implementation protects you against invalid data or changes to the enum definition, which will simplify debugging.

这两个gcc和clang(和可能的情况下)其他良好的编译器)将优化切换到二进制搜索或跳转表,取决于如何排序备选项和目标平台的精确特性。在任何情况下都不会对每个可能的值执行线性检查,例如 if ... else if ... else if ... 选项,这是几乎肯定是最慢的。

Both gcc and clang (and probably other good compilers) will optimize the switch into either a binary search or a jump table, depending on how the alternatives are ordered and the precise characteristics of the target platform. In neither case will it simply do a linear sequence of checks with each possible value, like the if ... else if ... else if ... option, which is almost certainly the slowest.

这样可以避免你考虑如何排序替换,特别是因为你可能需要不同的布尔函数。除非您是计算机体系结构方面的专家,否则您可以合理地假设您的编译器能够更好地理解它。

That saves you from thinking about how to order the alternatives, particularly since you might need various boolean functions with different orderings. Unless you are an expert in computer architectures, you can reasonably assume that your compiler understands it better.

这篇关于C ++函数static local,performance?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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