2011-03-25 15 views
0

我想增加更新()的性能;功能如下。 mathNumber變量內的數字將來自一個從文本字段創建的NSString。即使我使用五個數字,我希望它能夠運行用戶插入到文本字段中的任何數量。有什麼方法可以加速update()中的代碼?與C和/或Objective-C?我也希望它能在Mac和iPhone上工作。解決從文本字段的數學方程

typedef struct { 
    float *left; 
    float *right; 
    float *equals; 
    int operation; 
} MathVariable; 

#define MULTIPLY 1 
#define DIVIDE 2 
#define ADD 3 
#define SUBTRACT 4 

MathVariable *mathVariable; 
float *mathPointer; 
float newNumber; 

void init(); 
void update(); 
float solution(float *left, float *right, int *operation); 

void init() 
{ 
    float *mathNumber = (float *) malloc(sizeof(float) * 9); 

    mathNumber[0] =-1.0; 
    mathNumber[1] =-2.0; 
    mathNumber[2] = 3.0; 
    mathNumber[3] = 4.0; 
    mathNumber[4] = 5.0; 
    mathNumber[5] = 0.0; 
    mathNumber[6] = 0.0; 
    mathNumber[7] = 0.0; 
    mathNumber[8] = 0.0; 

    mathVariable = (MathVariable *) malloc(sizeof(MathVariable) * 4); 

    mathVariable[0].equals = &mathPointer[5]; 
    mathVariable[0].left = &mathPointer[2]; 
    mathVariable[0].operation = MULTIPLY; 
    mathVariable[0].right = &mathPointer[3]; 

    mathVariable[1].equals = &mathPointer[6]; 
    mathVariable[1].left = &mathPointer[1]; 
    mathVariable[1].operation = SUBTRACT; 
    mathVariable[1].right = &mathPointer[5]; 

    mathVariable[2].equals = &mathPointer[7]; 
    mathVariable[2].left = &mathPointer[0]; 
    mathVariable[2].operation = ADD; 
    mathVariable[2].right = &mathPointer[6]; 

    mathVariable[3].equals = &mathPointer[8]; 
    mathVariable[3].left = &mathPointer[7]; 
    mathVariable[3].operation = MULTIPLY; 
    mathVariable[3].right = &mathPointer[4]; 

    return self; 
} 

// This is updated with a timer 
void update() 
{ 
    int i; 

    for (i = 0; i < 4; i++) 
    { 
     *mathVariable[i].equals = solution(mathVariable[i].left, mathVariable[i].right, &mathVariable[i].operation); 
    } 

    // Below is the equivalent of: newNumber = (-1.0 + (-2.0 - 3.0 * 4.0)) * 5.0; 
    // newNumber should equal -75 
    newNumber = mathPointer[8]; 
} 

float solution(float *left, float *right, int *operation) 
{ 
    if ((*operation) == MULTIPLY) 
    { 
     return (*left) * (*right); 
    } 
    else if ((*operation) == DIVIDE) 
    { 
     return (*left)/(*right); 
    } 
    else if ((*operation) == ADD) 
    { 
     return (*left) + (*right); 
    } 
    else if ((*operation) == SUBTRACT) 
    { 
     return (*left) - (*right); 
    } 
    else 
    { 
     return 0.0; 
    } 
} 

編輯:

我首先要表示感謝您的所有類型的職位。這是我得到的第一個論壇,他們不告訴我我是個完全白癡。對自己的回報感到抱歉;我沒有意識到這也是一個客觀的C論壇(因此我匆忙使用C)。我有我自己的解析器,它很慢,但我不關心它的速度。我所需要的是加速update()函數,因爲它減慢了一切,90%的對象使用它。另外,我試着讓它在iOS設備上更快運行,因爲我無法在文本框中編譯任何東西。如果你有任何其他建議,讓更快()更快我感謝你。再次

感謝, 喬納森

編輯2:

嗯,我知道了通過更改它運行得更快:

int i; 

for (i = 0; i < 4; i++) 
{ 
*mathVariable[i].equals = solution(*mathVariable[i].left, *mathVariable[i].right, mathVariable[i].operation); 
} 

要:

*mathVariable[0].equals = solution(*mathVariable[0].left, *mathVariable[0].right, mathVariable[0].operation); 
*mathVariable[1].equals = solution(*mathVariable[1].left, *mathVariable[1].right, mathVariable[1].operation); 
*mathVariable[2].equals = solution(*mathVariable[2].left, *mathVariable[2].right, mathVariable[2].operation); 
*mathVariable[3].equals = solution(*mathVariable[3].left, *mathVariable[3].right, mathVariable[3].operation); 

有任何其他方式來增加它像陣列中的預加載數字一樣快在上面?

+0

你已經在C函數中有'return self',看起來像'mathNumber'和'mathPointer'之間的混淆......我猜這不是真正的代碼? – CRD 2011-03-25 20:11:02

+1

評估字符串爲數學?有一些代碼... http://github.com/davedelong/DDMathParser – 2011-03-25 20:27:22

回答

0

您的代碼是多種樣式的混合,並且包含指針的一些不當使用(例如,當傳遞operationsolution時)。目前還不清楚你爲什麼通過引用傳遞浮點數,但也許你打算改變這些變化並重新評估表達式?

下面是整理和偶然加速它的一些變化 - 任何這些成本不高,你可能會過早優化的罪過。正如@Dave評論說有些庫可以爲你解析,但是如果你的目標是簡單的數學表達式,那麼運算符優先級的基於棧的解析器/評估器就足夠容易編碼。

建議1:使用enum - 清潔:

typedef enum { MULTIPLY, DIVIDE, ADD, SUBTRACT } BinaryOp; 

typedef struct 
{ 
    float *left; 
    float *right; 
    float *equals; 
    BinaryOp operation; 
} MathVariable; 

建議2:使用switch - 清潔,大概也更快:

float solution(float left, float right, int operation) 
{ 
    switch(operation) 
    { 
     case MULTIPLY: 
      return left * right; 
     case DIVIDE: 
      return left/right; 
     case ADD: 
      return left + right; 
     case SUBTRACT: 
      return left - right; 
     default: 
      return 0.0; 
    } 
} 

注意我也刪除傳遞指針,調用的是現在:

*mathVariable[i].equals = solution(*mathVariable[i].left, 
            *mathVariable[i].right, 
            mathVariable[i].operation); 

現在OO的人可能會反對(: - ))到switch(或if/else),並爭辯每個節點(您的MathVariable)應該是知道如何執行自己的操作的實例。 C人可能會建議您在節點中使用函數指針,以便他們可以執行自己的操作。所有這些都是設計,你必須自己弄清楚。