Skip to content

C# too slow for math algos compared to c++ - can i implement asm or c++ code in c# forms? (like c++ lets you have __asm{ })

Rob Jones

Asked Jan 30, 2006, 3:10 AM EST1 answer
Share
LinkedInX
is there a way to speed up the math process of c#? i know c++ and asm are very quick at math algos but c# takes for ever (like hashing files). i know in c++ you could write __asm { mov eax,12345678 } and so on but is there a way in c# to do that or is it all in this slow ms intermediate language...

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hey, c# isn't necessarily much slower. The MSIL code is compiled to optimized machine code before it is run. I suggest you read http://www.grimes.demon.co.uk/dotnet/man_unman.htm for some of the finer points. If it's the garbage collection that is slowing things down you can implement IDisposable to take back some control over the memory management. If things are still too slow you can call unmanaged code (either C++ or assembler). There are two ways you can do this: 1) Use P/Invoke to call an unmanaged dll directly 2) Write a managed C++ wrapper around unmanaged C++, then add a reference to the managed C++ assembly. There are plenty of examples of this kind of stuff all over the place. I'm not sure if there are any speed considerations with P/Invoke (due to the marshaling). I would avoid using it for frequent calls. Hope this helps. Chris
    0