0

I have a program that looks like this:

struct EventTypeA {
    int someInt;        // random between 0 and 9
};

struct EventTypeB {
    int someOtherInt;   // random between 0 and 100000
};

int EventAHandler(EventTypeA e) {
    // Updates multiplier
    static int multipler = e.someInt;
    return multiplier;
}

double EventBHandler(EventTypeB e) {
    /* This is a simple example for illustration, the actual intermediate
       calculation takes up much more computational time than this */
    int intermediateResult = (e.someOtherInt * multipler) % 10 + 1;

    if (intermediateResult <= 3) { DoAction1(); }
    if (intermediateResult >= 7) { DoAction2(); }
}

...
...

void SomeMethodWithinSomeClass() {
    while (true) {
        // Listen for events of type A and B
        // if EventTypeA occurs, invoke EventAHandler
        // if EventTypeB occurs, invoke EventBHandler
    } 
}

I would like to have EventAHandler precompute a lookup table of the intermediateResult for all possible EventTypeB.someOtherInt's each time that an EventTypeA arrives and I have a new multiplier, so I can replace the calculation of intermediateResult in EventBHandler with a lookup.

The rationale for this is that my EventBHandler is time-sensitive, while EventAHandler is not: so when an EventTypeB arrives later, EventBHandler does not have to execute int intermediateResult = (e.someOtherInt * multipler) % 10 + 1; (let's pretend this statement takes up a lot more clock cycles) and only has to lookup.

My problem is that it only performs well if EventTypeA's occur frequently while EventTypeB's rarely occur.

At instances when several successive EventTypeB's occur, faster than I can pre-compute the lookup table, I want to kill the precomputation prematurely and switch back to the original approach. Is there a clean way to do this?

2 Answers 2

1

You have an option of updating your formula for calculating the intermediate results as:

intermediate_result = ((e.someOtherInt) % 10) * multiplier + 1

Now the first result would be in the range 0-10 and multiplier itself is also in the same range. Now it should be easy for you to have a 10x10 lookup table for the values. Although with the above formula, the actual calculation won't be that sub-optimal itself.

1
  • I gave an upvote for the clever mathematical manipulation, not the final answer I was expecting though because the actual intermediate calculation is much more computationally-expensive than this (something like multiple calls to a linear algebra library and large matrix manipulations). It's my fault for not clarifying this though, so I will update the question. Thanks!
    – elleciel
    Feb 8, 2014 at 8:58
1

You may pre-compute once the 0..9 * 0..100000 values. (You wanted EventAHandler does 1/10th of that job each time...)

then EventBHandler does just a lookup. EventAHandler won't do the huge computation each time.

If the pre-computated array doesn't fit in memory, you may use a database.

EventAHandler unsets a flag when retrieving part of the array and sets a flag when ready. EventBHandler computes the value if flag is not ready, else uses the lookup table.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.