-1

A project (C++11) I am working on involves a block of code that will be run somewhere in the trillions of times. I have an integer parameter B in [1,N] and points 1 = b1 < b2 < ... < bk = N where the code executes a different small block of code depending on which interval [bi, b(i+1)) B lies in. The only value that is changing throughout execution is B. However while the value of the bi's are fixed, they are only determined at runtime.

The naive thing to do is to write a bunch of if and else if statements, which at worst case involves k comparisons. However one can do this in constant time: construct a vector myGotos of size N and on each interval [bi, b(i+1)) store the location of the corresponding code block. Then you just do goto myGotos[B].

The solution above seems to me like it would be on average quicker, but the code would be quite ugly. I was wondering if there is a better way to do this.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
RghtHndSd
  • 117
  • 5

2 Answers2

1

The common way to do this is with a switch statement

switch(B){
case b1:..//
        break;
}

If you can declare those sections of code as lambdas or std::function provided they took the same arguments.Even a templated function might be ok. Its tough to answer without knowing what you actually need to run these functions.

map<int,decltype(yourLambda)>

Seems like it would work ok as well.

West
  • 722
  • 7
  • 16
  • Thank you for the answer. I neglected to mention that the value of the bi's are only determined at runtime, so a switch statement won't work. I have edited my question to include this. In any case, am I incorrect in saying that switch statement does a comparison for each case? If this is indeed true, then it would not give me constant time anyways. – RghtHndSd Oct 27 '15 at 16:32
  • You are incorrect it does a jump directly to the specified case. – West Oct 27 '15 at 16:33
  • Thank you for the correction. I should have considered using lamba for this. I like where you're going with the map idea, this should be what I'm looking for. Once I'm sure by tomorrow, I should be accepting this as an answer. – RghtHndSd Oct 27 '15 at 16:35
1

Initialize an array of N slots, let K, where each slot contains the index of the containing interval.

Then

switch (K[B])
{
case 1: // [B1,B2)
...
}
  • I tried implementing this. I randomly select 5 branch points on [1, 100), and then 100 million random numbers in [1,100). Now in the time comparison, even though I do _not_ include the construction of K, the naive "if else" method beats this method. I am really perplexed. Can the cost of accessing a vector be that great? I am using g++ 4.8.4 on linux with the -O2 option. – RghtHndSd Oct 27 '15 at 19:50
  • With 10 branch points, the naive version is no longer faster than this method. – RghtHndSd Oct 27 '15 at 19:56