473,320 Members | 1,987 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,320 software developers and data experts.

Speed of class instantiation (immediate assignment vs. default values)

Hello!

When declaring variables without immediately assigning a value, the CLR
(please correct here) assigns a default value according to the (reference)
type of the variable (e.g. string = null, int = 0 etc.).

I always assign default values to my variables, but am unsure whether this
degrades the performance of the instantiation of my classes.

Are the following two classes as fast to instantiate?

public abstract class CmsObjectNode : CmsObject, IXml
{
private Guid cmsObjectID = Guid.Empty;
private string cmsName = null;
...
}

public abstract class CmsObjectNode : CmsObject, IXml
{
private Guid cmsObjectID;
private string cmsName;
...
}

I would assume so, as the variables - after class instantion - yield the
immediate assigned value.

Thanks in advance.

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #1
3 1995
Anders... Inline
When declaring variables without immediately assigning a value, the CLR(please correct here) assigns a default value according to the
(reference)type of the variable (e.g. string = null, int = 0 etc.).<

Not!

Class1 c1;
Class1 c2= null;
Class1 c3= new Class1();
// OK
Console.WriteLine(c3.ToString());
// Runtime error: System NullReferenceException
Console.WriteLine(c2.ToString());
// Compile time error: Use of unassigned local variable
Console.WriteLine(c1.ToString());
Are the following two classes as fast to instantiate?


public abstract class CmsObjectNode : CmsObject, IXml
{
private Guid cmsObjectID = Guid.Empty;
...
}

public abstract class CmsObjectNode : CmsObject, IXml
{
private Guid cmsObjectID;
...
<

In embedded C I may make a decision based on efficiency. In C# I would
favor the explicit initialization since the code is clear and easy to
read, efficiency be {return "What did the fish say when it hit the end
of the reservoir?;}

Regards,
Jeff
{Dam}:)

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #2
Hello!
In embedded C I may make a decision based on efficiency. In C# I would
favor the explicit initialization since the code is clear and easy to
read, efficiency be {return "What did the fish say when it hit the end
of the reservoir?;}


Great, I'll keep that in mind and continue what I'm doing. Again, which
version was the fastest? :-)

BTW. What did the fish say? ;-)

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #3
Anders... Here is the IL from a default declaration of an int, bool and
char:

.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 7 (0x7)
.maxstack 1
IL_0000: ldarg.0
IL_0001: call instance void [mscorlib]System.Object::.ctor()
IL_0006: ret
} // end of method ClassDefault::.ctor

Here is the IL from a initialization of an int, bool and char:

.method public hidebysig specialname rtspecialname
instance void .ctor() cil managed
{
// Code size 28 (0x1c)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldc.i4.1
IL_0002: stfld int32 TestIL.ClassInit::i
IL_0007: ldarg.0
IL_0008: ldc.i4.1
IL_0009: stfld bool TestIL.ClassInit::b
IL_000e: ldarg.0
IL_000f: ldc.i4.7
IL_0010: stfld char TestIL.ClassInit::c
IL_0015: ldarg.0
IL_0016: call instance void [mscorlib]System.Object::.ctor()
IL_001b: ret
} // end of method ClassInit::.ctor

The first IL is faster.

Regards,
Jeff
Again, which version was the fastest? :-)<

{.00186 milliseconds faster actually}:)
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4

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

Similar topics

8
by: Rob Ristroph | last post by:
I have tried out PHP 5 for the first time (with assistance from this group -- thanks!). The people I was working with have a site that uses lots of php objects. They are having problems with...
11
by: Markku Uttula | last post by:
I think I'm doing something wrong. I'm able to connect to Oracle just fine, execute queries and all, but I'm having serious problems with the speed :( For example, the following PHP-script on my...
50
by: Dan Perl | last post by:
There is something with initializing mutable class attributes that I am struggling with. I'll use an example to explain: class Father: attr1=None # this is OK attr2= # this is wrong...
7
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename();...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
10
by: John A Grandy | last post by:
Say I have Class1 which contains static Class2 var1 = new Class2(); Is Class2 constructor code only executed if var1 is referenced in the code-execution path ? Or is Class2 constructor code...
10
by: Joel | last post by:
Is it true that if we don't specify a default constructor for our class, then the C# compiler provides us with its own that zeroes (or assigns default values) to the data members? I wrote a...
13
by: John Dann | last post by:
A Python newbie, but some basic understanding of how classes, objects etc work in eg VB.Net. However, I'm struggling a little to translate this knowledge into the Python context. I'm trying to...
8
by: hill.liu | last post by:
Hi, I stuck into this problem that I can't figure it out. Here is the class definition: class ctest { public: ctest(void) { cout << "ctest default constor" << endl; }; ctest(ctest& c) {...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.