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 7 1574
Fernando Cacciola wrote: 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?
Are you running your tests inside of VS.NET? If so, the JIT is not
performing any optimizations, even when you've compiled with
optimizations and you're not using the debugger.
Rerun your tests outside of VS.NET, see if that makes any difference. TIA
Fernando Cacciola
--
mikeb
"mikeb" <ma************@mailnull.com> escribió en el mensaje
news:ui**************@TK2MSFTNGP11.phx.gbl... Fernando Cacciola wrote:
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?
Are you running your tests inside of VS.NET? If so, the JIT is not performing any optimizations, even when you've compiled with optimizations and you're not using the debugger.
Rerun your tests outside of VS.NET, see if that makes any difference.
Ah! It must be that.. I run the code from within VS.NET.
Thanks!
Fernando Cacciola
"Fernando Cacciola" <fe***************@hotmail.com> wrote in message news:Ol*************@TK2MSFTNGP10.phx.gbl... Ah! It must be that.. I run the code from within VS.NET.
Did that make a difference?
--
Michael Culley
"Michael Culley" <mc*****@NOSPAMoptushome.com.au> escribió en el mensaje
news:O1**************@TK2MSFTNGP10.phx.gbl... "Fernando Cacciola" <fe***************@hotmail.com> wrote in message
news:Ol*************@TK2MSFTNGP10.phx.gbl... Ah! It must be that.. I run the code from within VS.NET.
Did that make a difference?
Not really....
Simply changing this
while ( col_beg.mPtr != col_end.mPtr )
with this
while ( !Col.Same(col_beg,col_end) )
makes the ellapsed time rise from about 2400 to 4400...
with
static public bool Same ( Col a, Col b ) { return a.mPtr != b.mPtr ; }
(I tried with 'ref' parameters too but that made no difference)
Any other idea? Or is it just that the JIT can't inline the simplest of the
functions?
Fernando Cacciola
Fernando Cacciola wrote: "Michael Culley" <mc*****@NOSPAMoptushome.com.au> escribió en el mensaje news:O1**************@TK2MSFTNGP10.phx.gbl...
"Fernando Cacciola" <fe***************@hotmail.com> wrote in message news:Ol*************@TK2MSFTNGP10.phx.gbl...
Ah! It must be that.. I run the code from within VS.NET.
Did that make a difference?
Not really....
Simply changing this while ( col_beg.mPtr != col_end.mPtr )
with this while ( !Col.Same(col_beg,col_end) )
makes the ellapsed time rise from about 2400 to 4400...
It seems to have a made a difference comparing numbers that you posted
in your first post and here. In your first post you were describing
timings that differed approximately 400% (2.7 vs. .58 seconds and 2.2
vs. .58 seconds). In this post, the numbers indicate about a 200%
difference.
I understand that this is still not as close as it should be, but it is
an indication that running outside of the VS.NET environment does make a
difference on the JIT output. with
static public bool Same ( Col a, Col b ) { return a.mPtr != b.mPtr ; }
I assume that the code for Same() is actually:
static public bool Same ( Col a, Col b ) { return a.mPtr == b.mPtr ; )
(I tried with 'ref' parameters too but that made no difference)
Any other idea? Or is it just that the JIT can't inline the simplest of the functions?
Finally, in looking over your original post, I noticed this time that
your Col struct is 'unsafe'. I wonder if the JIT does not inline unsafe
code, even if it could correctly do so.
Fernando Cacciola
--
mikeb
"mikeb" <ma************@mailnull.com> escribió en el mensaje
news:OQ**************@TK2MSFTNGP11.phx.gbl... Fernando Cacciola wrote: "Michael Culley" <mc*****@NOSPAMoptushome.com.au> escribió en el mensaje news:O1**************@TK2MSFTNGP10.phx.gbl...
"Fernando Cacciola" <fe***************@hotmail.com> wrote in message news:Ol*************@TK2MSFTNGP10.phx.gbl...
Ah! It must be that.. I run the code from within VS.NET.
Did that make a difference?
Not really....
Simply changing this while ( col_beg.mPtr != col_end.mPtr )
with this while ( !Col.Same(col_beg,col_end) )
makes the ellapsed time rise from about 2400 to 4400...
It seems to have a made a difference comparing numbers that you posted in your first post and here. In your first post you were describing timings that differed approximately 400% (2.7 vs. .58 seconds and 2.2 vs. .58 seconds). In this post, the numbers indicate about a 200% difference.
Yes, very good point.
I understand that this is still not as close as it should be, but it is an indication that running outside of the VS.NET environment does make a difference on the JIT output.
Right. I expected to see a similar performance, but as you say, there's no
doubt that running outside the IDE makes a big difference. with
static public bool Same ( Col a, Col b ) { return a.mPtr != b.mPtr ; }
I assume that the code for Same() is actually:
static public bool Same ( Col a, Col b ) { return a.mPtr == b.mPtr ; )
oh, yes, of course (I tried with 'ref' parameters too but that made no difference)
Any other idea? Or is it just that the JIT can't inline the simplest of
the functions?
Finally, in looking over your original post, I noticed this time that your Col struct is 'unsafe'. I wonder if the JIT does not inline unsafe code, even if it could correctly do so.
Good point.
I'll keep looking.
Maybe there are other factors like GC or so being involved.
I'll try to code a better benchmark interleaving both versions to account
for GC interruptions and other subtelties.
I'll get back with the new results.
Thanks
Fernando Cacciola
Well, it turned out that there was something getting in the way of my
previous benchmarks.
I rewrote it, and running from outside VS.NET I could see that there's no
performance penalty for such tiny easy to inline functions after all.
I tried with properties (both get/set), static/non-static methods and
trivial constructors.: they all seems to be properly inlined as I expected.
Thanks
Fernando Cacciola This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alex Gerdemann |
last post by:
Hello,
I have spent a bunch of time converting a Java program I wrote to C++ in
order to improve performance, and have found that it is not necessarily
faster.
Specifically, I'm writing a...
|
by: kk_oop |
last post by:
Hi. I wanted to use exceptions to handle error conditions in my code.
I think doing that is useful, as it helps to separate "go" paths from
error paths. However, a coding guideline has been...
|
by: Sheila Jones |
last post by:
Hello,
Can anybody tell me if the C# compiler will automatically inline simple
functions? To be specific, given:
double RadiansToDegrees(double degrees) {
return degrees*180.0/Math.PI;
}
...
|
by: LuB |
last post by:
How judicious ought one be when inlining small methods.
I once read that in general, most compiles will only inline 'one'
level.
IE: if all the following methods were declared/defined as...
|
by: atlaste |
last post by:
Hi,
I'm currently developing an application that uses a lot of
computational power, disk access and memory caching (to be more exact:
an information retrieval platform). In these kind of...
|
by: Michael Hull |
last post by:
Hi,
I remember from somewhere reading that inlining constructors is a
'BadThing', but now can't seem to find the original source. I can't
however thing of a reason why it would be for simple...
|
by: galiorenye |
last post by:
Hi,
Given this code:
A** ppA = new A*;
A *pA = NULL;
for(int i = 0; i < 10; ++i)
{
pA = ppA;
//do something with pA
|
by: colin |
last post by:
Hi,
I profile my code and find its spending a lot of time doing implicit
conversions from similar structures.
the conversions are mainly things like this
class Point
{
implicit conversion...
|
by: =?ISO-8859-1?Q?Konrad_M=FChler?= |
last post by:
Hi,
I've a list of objects. I iterate the list and read the value of each
object for many operation like:
x = myList.value1 + myList.value2
etc.
My question:
Is it efficient to always use...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |