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.
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.
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.
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.
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).
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.
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.