473,808 Members | 2,885 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Performance question regarding inlining of very simple struct methods

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_b eg,col_end) )
//while ( col_beg.mPtr != col_end.mPtr )
{
.... some code here...
col_beg.MoveRig ht();
//col_beg.mPtr += 3 ;
}

I'm compiling with Optimizations on, (with TRACE but no DEBUG) and using
QueryPerformanc eCounter() 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
Nov 15 '05 #1
7 1594
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_b eg,col_end) )
//while ( col_beg.mPtr != col_end.mPtr )
{
.... some code here...
col_beg.MoveRig ht();
//col_beg.mPtr += 3 ;
}

I'm compiling with Optimizations on, (with TRACE but no DEBUG) and using
QueryPerformanc eCounter() 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

Nov 15 '05 #2

"mikeb" <ma************ @mailnull.com> escribió en el mensaje
news:ui******** ******@TK2MSFTN GP11.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_b eg,col_end) )
//while ( col_beg.mPtr != col_end.mPtr )
{
.... some code here...
col_beg.MoveRig ht();
//col_beg.mPtr += 3 ;
}

I'm compiling with Optimizations on, (with TRACE but no DEBUG) and using
QueryPerformanc eCounter() 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

Nov 15 '05 #3
"Fernando Cacciola" <fe************ ***@hotmail.com > wrote in message news:Ol******** *****@TK2MSFTNG P10.phx.gbl...
Ah! It must be that.. I run the code from within VS.NET.


Did that make a difference?

--
Michael Culley
Nov 15 '05 #4

"Michael Culley" <mc*****@NOSPAM optushome.com.a u> escribió en el mensaje
news:O1******** ******@TK2MSFTN GP10.phx.gbl...
"Fernando Cacciola" <fe************ ***@hotmail.com > wrote in message

news:Ol******** *****@TK2MSFTNG P10.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_b eg,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


Nov 15 '05 #5
Fernando Cacciola wrote:
"Michael Culley" <mc*****@NOSPAM optushome.com.a u> escribió en el mensaje
news:O1******** ******@TK2MSFTN GP10.phx.gbl...
"Fernando Cacciola" <fe************ ***@hotmail.com > wrote in message
news:Ol******** *****@TK2MSFTNG P10.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_b eg,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

Nov 15 '05 #6

"mikeb" <ma************ @mailnull.com> escribió en el mensaje
news:OQ******** ******@TK2MSFTN GP11.phx.gbl...
Fernando Cacciola wrote:
"Michael Culley" <mc*****@NOSPAM optushome.com.a u> escribió en el mensaje
news:O1******** ******@TK2MSFTN GP10.phx.gbl...
"Fernando Cacciola" <fe************ ***@hotmail.com > wrote in message
news:Ol******** *****@TK2MSFTNG P10.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_b eg,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
Nov 15 '05 #7
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

Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
4286
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 circuit simulator, so I must first parse the netlist file. The program goes through an input file, and makes a hash_set of unique nodes (std::string's). The words are then sorted and numbered by copying the hash_set into a vector and sorting. ...
59
4457
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 presented that says "Use conventional error-handling techniques rather than exception handling for straightforward local error processing in which a program is easily able to deal with its own errors." By "conventional error-handling," I believe...
10
2352
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; } will something like
21
1803
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 inline in their respective classes, can I expect the compiler to try and inline them all? obj.GetSize()
13
4610
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 applications the last thing that remains is bare performance tuning. So for example, you can do an 'if then else' on a bit like a 'case/ switch', an 'if/then/else' and as a multiplication with a static buffer. Or, you can do sorting with an inline...
21
2055
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 constructors... any thoughts Mike
30
3555
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
10
1897
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 to vector3; //this conversion just returns positon Vector3 position;
8
1627
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 myList or should I get the pointer to
0
10631
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10114
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3011
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.