tags:

views:

111

answers:

5

I want to know which is beneficial in terms of performance and execution time: calling C code from C# or converting C code into C# code.

Note: My C code uses Linear Algebra Library.

+4  A: 

In terms of performance unmanaged code is supposed to be faster than managed code, at least that's what the theory says (depending on the scenario this might or might not be the case, only profiling could show). Now if you have many calls between those two worlds marshaling will occur which will slow down your code and probably render it less performant than a 100% managed solution. If on the other hand you have let's say a single call to an unmanaged function that does the heavy lifting it will probably perform better. So if you decide to go the interop path make sure you limit the number of calls to a minimum.

Darin Dimitrov
That's roughly the answer I had in mind, but you were faster ;)
Thomas Levesque
A: 

The only real answer is "it depends". How often are you calling the C code? Are you passing huge data structures back and forth? Are there C'isms in the code that'd take lots of extra processing to emulate in C#? There are dozens of other questions that come into play here.

I will mention, though, that .net is not nearly as slow as you seem to think.

cHao
+1  A: 

First, make sure that's where you need to optimize.

Then, write and measure both solutions (or more if you can) on your deployment computer(s).

pmg
+1  A: 

The only way to have a definitive answer to your specific case is to do actual performance timings for both. Having said that, I'd recommend using a more C# approach for most cases. The fact is that managed code is extremely fast. Also, there is going to be a performance penalty by going from managed code and calling out to unmanaged code.

Bottom line, default approach should be to keep everything in managed code IMO. Only look to write something in pure C if situation truly calls for it. There are many extremely fast and high performance solution in Production today in pure managed C# code.

Steve Michelotti
Alas, it depends. I'm doing a lot of numerics and found that the highly optimized libraries like Intel's MKL (or ACML, Atlas, Netlib etc.) are still unparalleled when it comes to pure number crunching. Apart from narrow, specific use cases I'd agree tough - managed code really **is** fast nowadays.
Jim Brissom
Nathan Ernst
@Steve, I agree with your general premise about only resorting to C "optimizations" if absolutely necessary, but the question seems to be whether to rewrite an existing C lib in C#. If the C libs are trivial to rewrite into C#, performance measurements are the only way to make an educated decision. If they're not trivial to reimplement, it's probably more productive to invoke the C code.
Nathan Ernst
A: 

In general, calling C code from C# is faster than the other way 'round. There is virtually no overhead in calling C functions; in fact, the OS functions are all called using C linkage. C# however needs some wrapper for the "managed" part, and again some wrapper for Datatypes.

If you have an existing C library that works fine, save yourself the trouble of porting it. You will only lose efficiency there - instead, make it usable from C# with some stubs and wrapper classes, that's the best solution if you have to or want to use C# with the existing C library.

Pure C code will be the fastest and most efficient way; however, you should be aware you'll lack many of the conveniences of C# if you chose to go that way.

foo