F
Fernando Cacciola
Hi, I'm terribly new at C# (come from C++ land).
I'm making some benchmarks to see the effect of different coding styles, and
I run across a situation that strikes me as pretty odd.
For my image processing code I would like to use low-level "Iterators", as I
would in C++, so I have a struct of the form:
public unsafe struct Col
{
public Col ( byte* ptr ) { mPtr = ptr ; }
static public bool Same( Col a, Col b ) { return a.mPtr == b.mPtr ; }
public void MoveRight() { mPtr += 3 ; }
public byte* mPtr ;
}
And a loop that looks like:
while ( !Col.Same(col_beg,col_end) )
//while ( col_beg.mPtr != col_end.mPtr )
{
.... some code here...
col_beg.MoveRight();
//col_beg.mPtr += 3 ;
}
I'm compiling with Optimizations on, (with TRACE but no DEBUG) and using
QueryPerformanceCounter() to measure execution time in ms.
If, for the while expression, I use the method 'Same', it takes about 2.7
seconds to complete a loop, but if I use direct comparison (see the
commented code right below), it takes about .58 seconds.
Likewise, using MoveRight() instead of the direct pointer addition takes 2.2
seconds with the direct pointer comparison for the while (that is, using the
configuration that took .58s and replacing only MoveRight())
Both MoveRight() and Same() are the kind of simple methods that I would have
expected the JIT to inline, so I'm kind of furstated by these results. Is
there any compiler options or switch or keyword I should have used?
TIA
Fernando Cacciola
I'm making some benchmarks to see the effect of different coding styles, and
I run across a situation that strikes me as pretty odd.
For my image processing code I would like to use low-level "Iterators", as I
would in C++, so I have a struct of the form:
public unsafe struct Col
{
public Col ( byte* ptr ) { mPtr = ptr ; }
static public bool Same( Col a, Col b ) { return a.mPtr == b.mPtr ; }
public void MoveRight() { mPtr += 3 ; }
public byte* mPtr ;
}
And a loop that looks like:
while ( !Col.Same(col_beg,col_end) )
//while ( col_beg.mPtr != col_end.mPtr )
{
.... some code here...
col_beg.MoveRight();
//col_beg.mPtr += 3 ;
}
I'm compiling with Optimizations on, (with TRACE but no DEBUG) and using
QueryPerformanceCounter() to measure execution time in ms.
If, for the while expression, I use the method 'Same', it takes about 2.7
seconds to complete a loop, but if I use direct comparison (see the
commented code right below), it takes about .58 seconds.
Likewise, using MoveRight() instead of the direct pointer addition takes 2.2
seconds with the direct pointer comparison for the while (that is, using the
configuration that took .58s and replacing only MoveRight())
Both MoveRight() and Same() are the kind of simple methods that I would have
expected the JIT to inline, so I'm kind of furstated by these results. Is
there any compiler options or switch or keyword I should have used?
TIA
Fernando Cacciola