B
Bill Woessner
I've been chewing on a problem and haven't come up with a good solution.
I'm hoping some of you have thought about it and come up with something
better than I have.
Suppose I have a mathematical step function like this:
f(x) = { 0, x <= x_1
1, x_1 <= x < x2
...
n, x >= x_n }
What's an efficient way to evaluate this in C++? Obviously, you can have
a huge if...else if...else statement. But after a while, that gets to be
really inefficient. I thought about using perfect hash (gperf), as you
would for optimizing a case statement, but the hash function would have to
be monotone since I'm dealing with ranges and that's not the case.
Currently, I've implemented it using a lookup table. It's significantly
faster but this only works because the knots (x_1, x_2, ..., x_n) are
integers. If, for example, x_1 = 1 and x_2 = pi, this wouldn't work.
This seems like a fairly ubiquitous problem so I'm hoping some of you
have come up with better solutions than I. Thanks in advance.
--Bill
I'm hoping some of you have thought about it and come up with something
better than I have.
Suppose I have a mathematical step function like this:
f(x) = { 0, x <= x_1
1, x_1 <= x < x2
...
n, x >= x_n }
What's an efficient way to evaluate this in C++? Obviously, you can have
a huge if...else if...else statement. But after a while, that gets to be
really inefficient. I thought about using perfect hash (gperf), as you
would for optimizing a case statement, but the hash function would have to
be monotone since I'm dealing with ranges and that's not the case.
Currently, I've implemented it using a lookup table. It's significantly
faster but this only works because the knots (x_1, x_2, ..., x_n) are
integers. If, for example, x_1 = 1 and x_2 = pi, this wouldn't work.
This seems like a fairly ubiquitous problem so I'm hoping some of you
have come up with better solutions than I. Thanks in advance.
--Bill