Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Just looking at this code, I'd suspect from my experience a fairly significant slowdown going from C++ -> C#.</p> <p>One major issue you're going to face in a naive port of a routine like this to C# is that C# is going to add bounds checking on every array check here. Since you're never looping through the arrays in a way that will get optimized (<a href="https://stackoverflow.com/questions/631123/what-is-the-special-case-with-the-foreach-loop-that-eliminates-bounds-checking">see this question for details</a>), just about every array access is going to receive bounds checking.</p> <p>In addition, this port is pretty close to a 1->1 mapping from C. If you run this through a good .NET profiler, you'll probably find some great spots that can be optimized to get this back to near C++ speed with one or two tweaks (that's nearly always been my experience in porting routines like this).</p> <p>If you want to get this to be at nearly identical speed, though, you'll probably need to convert this to unsafe code and use pointer manipulation instead of directly setting the arrays. This will eliminate all of the bounds checking issues, and get your speed back.</p> <hr> <p>Edit: I see one more huge difference, which may be the reason your C# unsafe code is running slower.</p> <p>Check out <a href="http://msdn.microsoft.com/en-us/library/yyaad03b.aspx" rel="nofollow noreferrer">this page about C# compared to C++</a>, in particular:</p> <p>"The long type: In C#, the long type is 64 bits, while in C++, it is 32 bits."</p> <p>You should convert the C# version to use int, not long. In C#, long is a 64bit type. This may actually have a profound effect on your pointer manipulation, because I believe you are inadvertantly adding a long->int conversion (with overflow checking) on every pointer call.</p> <p>Also, while you're at it, you may want to try wrapping the entire function in an <a href="http://msdn.microsoft.com/en-us/library/a569z7k8.aspx" rel="nofollow noreferrer">unchecked block</a>. C++ isn't doing the overflow checking you're getting in C#.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload