可能的代码速度优化?

时间:2013-08-07 09:34:39

标签: c++ variables optimization error-handling static

好的,我有一个我的Trig类,用于存储值的静态表,以便在我的程序中更快地执行sin,cos和tan函数。我目前的方法是否有任何评论和/或速度改进?感谢以前的答案,我已经对C ++感觉更有能力了。

Trig.h

#pragma once
#include <math.h>

class Trig
{
private:
    struct Table
    {
        static const int multiple = 10; // Accurately stores values to 1/10 of a degree
        double sin[360*multiple];
        Table();
    };
    static const Table table;
    static void shrinkRange(double*); // Shrinks degrees to range 0-359 for proper array indexing
public:
    static double sin(double);
    static double cos(double);
    static double tan(double);
};

Trig.cpp

#include "Trig.h"

Trig::Table::Table() // table constructor
{
    double const PI = 3.14159265358979323;
    double const degToRad = PI/180.0;
    double const incr = 1.0/multiple;
    int index = 0;
    for (double angle = 0; index != 360*table.multiple; angle += incr)
        Table::sin[index++] = _INC_MATH::sin(angle*degToRad);
}

Trig::Table const Trig::table; // initialize static table member

void Trig::shrinkRange(double* degrees)
{
    if (*degrees >= 360)
        *degrees -= 360*( (int)*degrees/360);
    if (*degrees < 0)
        *degrees += 360*( -(int)*degrees/360 + 1);
}

double Trig::sin(double degrees)
{
    shrinkRange(&degrees);
    degrees *= table.multiple;
    return Trig::table.sin[(int)(degrees+0.5)];
}

double Trig::cos(double degrees)
{
    return Trig::sin(degrees + 90);
}

double Trig::tan(double degrees)
{
    return Trig::sin(degrees)/Trig::cos(degrees);
}

5 个答案:

答案 0 :(得分:2)

C ++不是Java。在这种情况下,您无法调用函数或 访问类上的成员,因为没有类对象; 您只需指定其范围即可访问静态成员:

Trig::createTable();
Trig::COS_TABLE[120];

此外(但在Java中也是如此),您可以自动进行 使用动态确保正确初始化 初始化。如果你真的想保持现状 结构,您可以添加如下内容:

bool initted = (Trig::createTable(), true);

在命名空间范围内的任何位置。更惯用的是定义 包含两个表的对象类型,带有构造函数 初始化它们,并声明一个静态实例:

class Trig
{
public:
    struct Tables
    {
        double sin[360];
        double cos[360];
        Tables();
    };
    static Tables const tables;
    //  ...
};

Trig::Tables const Trig::tables;

Trig::Tables::Tables()
{
    double rad = PI / 180.0;
    for ( int angle = 0; angle != 360; ++ angle ) {
        sin[angle] = std::sin( angle * rad );
        cos[angle] = std::cos( angle * rad );
    }
}

无需明确调用Trig::createTable;编译器 为你照顾这个。

(Java中提供了类似的技术。出于某些原因, 它们并不经常使用,因为上述内容非常惯用 C ++。)

答案 1 :(得分:0)

Trig::createTable();
double x = Trig::COS_Table[120];

答案 2 :(得分:0)

要访问静态,您需要使用:: 即。

Trig::COS_TABLE

而不是Trig.COS_TABLE

但是,只包含静态的类在C ++中有点奇怪。

答案 3 :(得分:0)

需要使用namespcae运算符::访问静态变量,这与Java中使用的.不同。

因此,要在不创建对象的情况下调用方法createTable(),请使用以下语法:

Trig::createTable();

由于SIN_TABLECOS_TABLE表也是静态的,请使用语法:

Trig::SIN_TABLE[120];
Trig::COS_TABLE[120];

答案 4 :(得分:0)

最好,让你的表(非静态)成员拥有一个静态对象:

#include <cmath>                         // include C++ header instead of C

namespace trig {                         // use a namespace instead of a class
  const double pi = 4*std::atan2(1.,1.); // avoid pre-processor macros
  namespace details {                    // hide details in inner namespace
    struct sin_cos_tables                // class holding tables
    {
      double SIN[360];
      double COS[360];
      sin_cos_tables()                   // default constructor: set tables
      {
        const double fac = pi/180;
        for(int angle = 0; angle != 360; ++angle)
        {
          SIN[angle] = std::sin(angle*fac);
          COS[angle] = std::cos(angle*fac);
        }
      }
    };
    static sin_cos_tables TABLES;        // static object constructed at start-up
  }
  static const double*SIN_TABLE = details::TABLES.SIN;  // for your convenience
  static const double*COS_TABLE = details::TABLES.COS;  // (not really necessary)
}

int main()
{
  double x = trig::SIN_TABLE[120];
}

这样,表就会在其他(非静态)代码运行之前自动创建。