tags:

views:

264

answers:

5

I am looking to create a custom math library for the project I am working on. The project is written in C#, and I am slightly concerned whether C# will be fast enough. The library will have a number of custom math formulas and equasions to be applied to very large data sets. Simulations and matrix operations will be done as well (i.e. Monte Carlo simulations) so it'd have to be fast.

One thought is to create the math library in C++ and reference this .dll within the C# project. I am wondering whether it is worth the effort?

+6  A: 

The general rule of thumb is "don't optimize until you need to," so I would lean towards just writing it in C# and optimizing the code later on.

But, in this situation where optimizing might require reimplementing everything in another language, I would do some testing first. Write a small app using the most processor-intensive math you expect in both C# and C++, then compare the times to see if the C# one is acceptable.

Tom Dalling
+1  A: 

It's a little hard to say anything definitive one way or the other. I'd suggest sticking with C# if that's what you've started or what the rest of your project is based around. Keep some canonical data sets aside and establish some benchmarks as you develop. If you find performance to fall below some unacceptable threshold, and your profiling leads you to believe the problem is intrinsic to C#, then write a C++ component to solve those specific needs.

ars
+5  A: 

If you will be using it in C#, then you might as well put it in C# to start with. You buy more with managed code than you save with pointer wrangling. If you are worried about memory and cache issues, then just use arrays of types instead of objects. It gives you more control over how the memory is laid out.

The optimizers and JIT compilers will buy you more than enough speed to make up for any inefficiencies.

William Crim
+1  A: 

One thing to keep in mind is that using bytecode languages like C# or Java lets the JIT compiler in the runtime optimise your code. In practice, this means that the runtime performance of your code only gets better over time. Unlike C++, where the machine code is produced once at compile time and never changes, the performance of your C# code can continuously improve along with improvements to the underlying JIT compiler.

A serious amount of research is going into JIT compiler technology these days. Taking advantage of this now is an excellent approach.

Greg Hewgill
The .Net JIT currently has no hotspot like mode so this will happen only as the Framework itself is improved (and this is by no means a guarantee on each new release though currently it is very likely to be the case).Java has HotSpot VM's (and even more aggressive ones like JRockit) which allow the currently running process to be re-jitted as the app runs. I assume you were talking abotu the former style of improvements rather than thelatter but just wanted to be clear
ShuggyCoUk
Right, I was generally referring to the former style, rather than an adaptive at-runtime recompilation. But the latter could easily come in the future too!
Greg Hewgill
A: 

One of the reasons I like using C# for numerical programming is that it is fairly easy to interface with native code. C# and the latest .NET runtimes and JIT compilers are pretty darn good, but sometimes you just can't beat highly optimized native code. For example, here is what I have done for linear algebra stuff. Write some nice object oriented classes that hide the implementation of key operations. For me this meant creating Matrix and Vector classes with addition and multiplication functions/operators. When I encountered an algorithm that had to perform several matrx-matrix products and transpose products with rather large matrices (thousands of rows by hundreds of columns) over many iterations, things got too slow. I re-implemented the matrix multiplication function to call a highly optimized matrix-matrix multiplication function from Intel's Math Kernel library (dgemm). This gave me a better than 20x speed up. Plus the unpleasant API for this native routine (dgemm takes no less than 13 parameters!) was hidden from users of the matrix class.

So, I would suggest using C# for your library and drop down to optimized native code when and where needed.

Steve Sneller