unless you've profiled the code and it turned out to be an obvious mistake, like using a vector in a place where you should have been using a map, whose search time is logarithmic vs. linear in case of vector.
There's a bit more to say in this topic actually, and it's really the place where discussions about speed make much sense, because for larger amount of computations it can make a difference of runtime of one hour vs. one year or more.
To show an example, last time we were discussing with ADSWNJ the following problem: Imagine that you're in a circular orbit and you want to deorbit your craft. You're interested in knowing where your potential landing site would be if you applied, say a -200 m/s retrograde burn at current frame. Therefore, you need to learn the 3D position vector at the place, where your orbit's position vector's length is equal to the radius of planet (in other words your orbit crosses the surface). You'd then convert this position vector to latitude and longitude using Orbiter API and viola.
Now how to get this position. My proposal was to use [ame="http://orbithangar.com/searchid.php?ID=3825"]KOST[/ame]. Looking at it from a broader perspective, KOST is a configurable (given initial state vectors) function, that translates time into position, so pos = KOST(t), where time is the amount of time passed from the moment of providing state vectors. As a side note, the state vectors' velocity component needs to be shortened by 200 m/s, as already said, before performing the KOST simulation. And here we have a typical problem. You don't know the optimal time which provides the required pos, ie. such that it crosses the planet's surface. You only know that the maximal time range is in (0, orbitPeriod/2), because the periapsis is at orbitPeriod/2, and if you don't reach surface then (|pos| > Radius), then it means that you'll never reach it with the dv of -200 m/s, and you need to decelerate more. But let's assume now that you do reach the surface in this time range.
Amateur's approach (Naive algorithm):
Iterate KOST function with a constant time step, like 10s and check on each iteration if (|KOST(t_i)| < Radius). If it does, you return pos = KOST(t_i). If the orbit's period was 6000, then T/2 = 3000, so pessimistically you needed 3000 / 10 = 300 iterations to solve this simple task. Let's assume that it turned out that 10s precision was giving you unstable results, and you need to increase the precision to 0.1s. You get 3000 / 0.1 = 30000 iterations! You'd surely start feeling this in your simulation. This was an example of linear complexity, or O

.
Pro's approach (Divide and Conquer):
Since you know the maximal time bound, which is T/2, you start off from calculating the KOST(t) for T/2 and note the sign of (KOST(t) - Radius), it should be negative in this case, because we said, that -200 m/s is enough. If it isn't, break the calculations, because you'll not reach the surface. Now slice the current reference time, T/2 into 2 partitions, and search for KOST(t) in the middle of the two partitions, so for t = T/2 - T/4 = T/4 and note the sign. If it has changed, then it means that the solution is in the furthest partition (between T/4 and T/2), and if it hasn't, then it's in the first partition (between 0 and T/4), let's assume that it's in the 2nd partition (realistic case). We slice it into next 2 partitions, and proceed into the middle of them, so t = T/4 + T/8 = 3T/8 and test KOST(t) there, and so on, until we reach the stated precision (difference between subsequent time slice values). The total number of iterations will be 2*log(maxArg / precision), so in our case
2*log(3000 / 0.1) = 21! This will be barely noticeable in your sim, because it's just a little bit of pure math, and no I/O operations. The general idea is to slice the problem into many binary sub problems, where the answer to each one is either true or false. This is an example of a binary search with logarithmic complexity, or O(log

), and this is where you should be spending most of your optimization effort, when in your app you need to guess an optimal argument of function numerically. You could do it analytically, but in general that works only in schools on unrealistically simplified examples. Naturally, if you're able to solve such complex problems analytically, then hats off.
I've used this technique in Direct Ascent recently, where my function is PEG, from which I try to acquire the engine thrust, that gives me a concrete time of orbital insertion, that is such that is equal to target's arrival on my insertion position, so deltaArrivalTime = PEG(engF). The delta time must be 0 in my case, not Radius, as in previous case, but it only means that in my case the reference value is 0, while in the previous case the reference value was Radius.
This problem is so generic, that I've decided to write a generic binary solver for such problems:
BinSearchArg.cpp
BinSearchArgSubject.hpp