473,395 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Reference Types Performance MS Peeps please sound off!

First, is null considered a Value Type or a Reference Type.

I have a question about performance and memory. Does the performance
change or the amount of memory that is consumed when you have a
difference in flow control statements as follows:

//Arbitrary Reference Type
Mailmessage mm = new Mailmessage();
//A
if (null != mm)
{
Console.WriteLine(mm.ToString());
}

Or

//B
if (mm != null)
{
Console.WriteLine(mm.ToString());
}

I have been told that A performs better since the CLR does not have to
make a copy of the value null, I think it doesn't matter since both
values must be checked regardless. (I also think this reads like crap)

Please Help!
Nov 13 '05 #1
1 1514
Quizical <Co************@yahoo.com> wrote:
First, is null considered a Value Type or a Reference Type.
null is a reference.
I have a question about performance and memory. Does the performance
change or the amount of memory that is consumed when you have a
difference in flow control statements as follows:

//Arbitrary Reference Type
Mailmessage mm = new Mailmessage();
//A
if (null != mm)
{
Console.WriteLine(mm.ToString());
}

Or

//B
if (mm != null)
{
Console.WriteLine(mm.ToString());
}

I have been told that A performs better since the CLR does not have to
make a copy of the value null, I think it doesn't matter since both
values must be checked regardless. (I also think this reads like crap)


Neither version uses null - the test compiles to

ldloc.0 // Put the value of mm on the stack
brfalse.s [whatever] // Skip the block if the topmost stack value is 0

Given that the IL is the same in both cases, the JITted code should be
the same too.

Oh, and I agree that B reads better too - and that, to me, would be far
more important than any tiny performance gains which could be had if A
were in fact slightly different. Micro-optimisations like this attempts
to achieve should not be used unless that section of code is *proven*
to be a bottleneck.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #2

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

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
2
by: Simon Storr | last post by:
Posting this again as it just got deleted off the server?! Is it possible to perform an 'in' set operator check on an enumerated type? For example something like enum WeekDays =...
8
by: Dennis Myrén | last post by:
I have these tiny classes, implementing an interface through which their method Render ( CosWriter writer ) ; is called. Given a specific context, there are potentially a lot of such objects,...
2
by: Anders Borum | last post by:
Hello! I was looking at marking objects with a changed state, once properties have been changed. The reason behind this is, that I would like to enlist such objects for processing in a...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
5
by: Daniel Oberhoff | last post by:
Hi, Can someone more proficient in the details of the c++ type system please tell me how I can instantiate the for constesses of references that I think should be possible: 1. non-constant...
8
by: Frank Hauptlorenz | last post by:
Hello out there, I changed an existing and good working webservice from an wsHttpBinding to an NetTcpBinding. This is working (after trying some time) and has real a better performance! But...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
marktang
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,...
0
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...
0
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...

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.