Click to See Complete Forum and Search --> : Will c++ increase computational performance?


klintan
April 20th, 2005, 08:25 AM
I'm writing a software that simulates forest growth, and it is very computing intense, e.g. for a number of trees it calculates mortality, growth, volume etc. using some quite complex formulas (typically the formulas have been created using regression analysis, and contain a number of variables that are multiplied with a number of constants).

The original version was written in Fortran 25 years ago, and I am currently re-writing it (and adding lots of new functionality) in c#. But obviously, the Fortran programs outperform my programs.

I have now come up with a number of options that would improve my performance. It has all to do with optimization of the most computing intense parts of the program, which could be done as follows:

1. Optimize the c# code, perhaps by moving all necessary variables to arrays instead of having them in objects and object collections
2. Move the critical parts to managed c++
3. Use one of the Fortran for .Net compilers available
4. Move the critical parts to a compiled dll, develop it in c++ (or Fortran), and call it via COM

Wouldn't 1-3 give the same result, don't all these methods produce IL, and the speed of the code just depends on how efficient my algorithms are?

4 could potentially be a lot faster, if I am able to move out large chunks of compuations. But, if the code in 1, 2, or 3 was efficient enough, maybe 4 wouldn't gain that much.

I read some thread where Java was compared to C in a large matrix operation, and Java was actually quite fast here.

Any comments or suggestions anyone?

marten_range
April 21st, 2005, 12:25 PM
C# can yield pretty good performance and it's no guarantee that switching to C++ will give you a major performance boost.

One thing to look out for in these situations is that new is a resource hog. Make sure that your algorithms doesn't allocate memory dynamically while the algorithms are running. If possible allocate enough memory for the worst-case case scenario.

The OO approach is to allocate memory dynamic but that's also one of the reasons Fortran gives better performance.

Object collections are using dynamic memory allocations below the surface when adding/removing items. Also notice that ArrayList in C# is only effecient at pushing/popping elements at the end of the list.

All, these points applies to C++ as well. C++ does however allow you to a greater extent work with value types and resizeable containers of such that can be more efficient.

(In C++ new is based on malloc/free which are even less effecient than new in C# so if you create lot of objects dynamically the C++ will suffer from this)