I am currently working on critical performance code, and I have a special situation where I would like to write an entire application in C #, but due to performance reasons, C ++ ends up faster than FAR.
I conducted several tests on two different implementations of some code (one in C #, the other in C ++), and the timings showed that the C ++ version was 8 times faster, both versions in release mode and with all optimizations turned on. (Actually, C # had the advantage of compiling as 64-bit. I forgot to include this in C ++ time)
So, I suppose, I can write most of the code base in C # (which C # is very easy to write), and then write my own versions of things where performance is critical. The particular piece of code that I tested in C # and C ++ was one of the critical areas where more than 95% of the processing time was spent.
What is the recommended wisdom when writing native code here? I never wrote a C # application that calls native C ++, so I have no idea what to do. I want to do this in such a way as to minimize the cost of having to make my own calls as much as possible.
Thanks!
Edit: The following is most of the code I'm actually trying to work on. This is for n-body modeling. 95-99% of the processor time will be spent on Body.Pairwise ().
class Body { public double Mass; public Vector Position; public Vector Velocity; public Vector Acceleration;
I also have a class that simply controls the simulation in the following ways:
public static void Pairwise(Body[] b, int n) { for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) b[i].Pairwise(b[j]); } public static void Predict(Body[] b, int n, double dt) { for (int i = 0; i < n; i++) b[i].Predict(dt); } public static void Correct(Body[] b, int n, double dt) { for (int i = 0; i < n; i++) b[i].Correct(dt); }
The main loop looks like this:
for (int s = 0; s < steps; s++) { Predict(bodies, n, dt); Pairwise(bodies, n); Correct(bodies, n, dt); }
Above is just the minimal larger application I'm working on. Something else happens there, but the most critical things happen in these three functions. I know that the paired function is slow (It n ^ 2), and I have other methods that are faster (Barnes-hutt for one, that n log n), but that goes beyond what I ask in this question.
C ++ code is almost identical:
struct Body { public: double Mass; Vector Position; Vector Velocity; Vector Acceleration; void Pairwise(Body &b) { Vector dr = b.Position - this->Position; double r2 = dr.LengthSq(); double r3i = 1 / (r2 * sqrt(r2)); Vector da = r3i * dr; this->Acceleration += (b.Mass * da); b.Acceleration -= (this->Mass * da); } void Predict(double dt) { Velocity += (0.5 * dt) * Acceleration; Position += dt * Velocity; } void Correct(double dt) { Velocity += (0.5 * dt) * Acceleration; Acceleration.Clear(); } }; void Pairwise(Body *b, int n) { for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) b[i].Pairwise(b[j]); } void Predict(Body *b, int n, double dt) { for (int i = 0; i < n; i++) b[i].Predict(dt); } void Correct(Body *b, int n, double dt) { for (int i = 0; i < n; i++) b[i].Correct(dt); }
The main loop:
for (int s = 0; s < steps; s++) { Predict(bodies, n, dt); Pairwise(bodies, n); Correct(bodies, n, dt); }
There is also a vector class that works just like a regular math vector, which I don't include for brevity.