Math variable speed

Nibbie
Posts: 1
Joined: 2011.03
Post: #1
I am trying to run 800,000 objects, but right now I can only get 200,000 running at a decent frame rate. I do not know what is bogging down the -update method. The variable mathNumber will come from an NSString in a text field. The only snippets of code that I cannot edit are the variables mathNumber[0 to 4], and the newNumber variable. Even though I'm using five numbers/variables I would like it to be able to run any amount that the user creates in a text field. Any help would be much appreciated.

Code:
********* Implementation *********

    // Even though there are five numbers in the equation below the NSString text field may have more or less.
    // newNumber = (-1.0 + (-2.0 - 3.0 * 4.0)) * 5.0;

    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;
    // The pointers below point to whatever the equations equal.
    mathNumber[5] = 0.0;
    mathNumber[6] = 0.0;
    mathNumber[7] = 0.0;
    mathNumber[8] = 0.0;

    theObject = [[Object alloc] initWithNumbers:mathNumber];

    // This is constantly updated with an NSTimer
    [theObject update];

********* In Another Header *********
typedef struct {
    float *left;
    float *right;
    float *equals;
    unsigned char operation;
} MathVariable;

#define PSC_MULTIPLY 1
#define PSC_DIVIDE 2
#define PSC_ADD 3
#define PSC_SUBTRACT 4

MathVariable *mathVariable;
float *mathPointer;
float newNumber;

- (id)init;
- (void)update;
float solution(float *left, float *right, unsigned char *operation);

********* In Another Implementation *********

// Methods and Functions for each object are below

- (id)initWithNumbers:(float *)mathPointer
{
    mathVariable = (MathVariable *) malloc(sizeof(MathVariable) * 4);

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

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

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

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

    return self;
}

// This is updated with an NSTimer
- (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, unsigned char *operation)
{
    if ((*operation) == PSC_MULTIPLY)
    {
        return (*left) * (*right);
    }
    else if ((*operation) == PSC_DIVIDE)
    {
        return (*left) / (*right);
    }
    else if ((*operation) == PSC_ADD)
    {
        return (*left) + (*right);
    }
    else if ((*operation) == PSC_SUBTRACT)
    {
        return (*left) - (*right);
    }
    else
    {
        return 0.0;
    }
}
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
Quote this message in a reply
Post Reply