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

4 questions about object declaration and its performance

1.The follow two ways to declare one object: any difference? especially its
performance.
a.Private m_objMyObject As MyObject=New MyObject()
b.Private m_objMyObject As MyObject
m_objMyObject=New MyObject

2.Any difference between a and b?
a.
For Each childItem In SomeItems
Dim objData As DataObject=m_objOneSource.GetData(childItem.ID)
Next
b.
Dim objData As DataObject
For Each childItem In SomeItems
objData=m_objOneSource.GetData(childItem.ID)
Next

3.Explicitly destroy an object will hurt the system performance rather than
improve it?
Dim o_objSomething As SomeObject
'do something
o_objSomething=Nothing

4.Any difference in performance/system resources usage between
public/private declaration of objects/functions?
For example:
a.public objOne as TheObject
b.private objOne as TheObject
--
Regards,
Unruled Boy
Jul 21 '05 #1
2 1621
I'll do my best to answer these questions.

1. I don't believe there are eny performance issues with this. There where
issues in VB6, but in .NET, there don't seem to be any issues.

2. b May be slightly more efficient, but you have to decide if you want to
run the possible risk of using the object outside it's intended scope..

3. This seems to have coused alot of discussion in the various newsgroups..
From what I understand, the GC *can* clean up an object even if you don't set
it to Null/Nothing. If the variable is no longer used (i.e. no code using the
variable) then the variable can be garbage collected even if it has not yet
lost scope. Having said that, it does not hurt your code to set it to
Null/Nothing..

4. I don't believe that there are any performance issues with
public/private. Probably more to the point is you should probably keep your
scope as small as possible. i.e If it is needed only internally to a class,
make it private, if it is needed by an external class, make it private with a
public property. Encapsulation is a core concept with OOP development.

Hope this helps a little..

Eddie de Bear
Cheers

"Unruled Boy" wrote:
1.The follow two ways to declare one object: any difference? especially its
performance.
a.Private m_objMyObject As MyObject=New MyObject()
b.Private m_objMyObject As MyObject
m_objMyObject=New MyObject

2.Any difference between a and b?
a.
For Each childItem In SomeItems
Dim objData As DataObject=m_objOneSource.GetData(childItem.ID)
Next
b.
Dim objData As DataObject
For Each childItem In SomeItems
objData=m_objOneSource.GetData(childItem.ID)
Next

3.Explicitly destroy an object will hurt the system performance rather than
improve it?
Dim o_objSomething As SomeObject
'do something
o_objSomething=Nothing

4.Any difference in performance/system resources usage between
public/private declaration of objects/functions?
For example:
a.public objOne as TheObject
b.private objOne as TheObject
--
Regards,
Unruled Boy

Jul 21 '05 #2
"Unruled Boy" <Un********@discussions.microsoft.com> schrieb
1.The follow two ways to declare one object: any difference? especially
its
performance.
a.Private m_objMyObject As MyObject=New MyObject()
b.Private m_objMyObject As MyObject
m_objMyObject=New MyObject
Depends on where you declare this.

this one:
Private m_objMyObject As MyObject=New MyObject()
may be declared in an Initializer (op-Code: .cctor) which is called BEFORE
the contructor is called.

this one:
Private m_objMyObject As MyObject
m_objMyObject=New MyObject
may only be called in a Contructor (op-Code: .ctor). Performance issues?
Depends on your type.
2.Any difference between a and b?
a.
For Each childItem In SomeItems
Dim objData As DataObject=m_objOneSource.GetData(childItem.ID)
Next
b.
Dim objData As DataObject
For Each childItem In SomeItems
objData=m_objOneSource.GetData(childItem.ID)
Next
Why don't you look at MSIL? You'll see that both ways emit the same
op-Codes.
3.Explicitly destroy an object will hurt the system performance rather
than
improve it?
Dim o_objSomething As SomeObject
'do something
o_objSomething=Nothing
If your types use extern resources (Database connection, Files, etc)
implement IDisposable. If a type you use from the FCL implements IDisposable
then use it.
You cannot determine when the Garbage Collector starts collecting your
objects.
Using GC.Collect() normally doesn't improve performance.
4.Any difference in performance/system resources usage between
public/private declaration of objects/functions?
For example:
a.public objOne as TheObject
b.private objOne as TheObject


No.

Cheers

Arne Janning
Jul 21 '05 #3

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

Similar topics

3
by: Balaji Kannan | last post by:
Hi, In dot net during component development i have used some member variables in the class file. Inside the class i have used the member declaration and the instant handling in the following...
2
by: Unruled Boy | last post by:
1.The follow two ways to declare one object: any difference? especially its performance. a.Private m_objMyObject As MyObject=New MyObject() b.Private m_objMyObject As MyObject m_objMyObject=New...
2
by: 1944USA | last post by:
I am re-architecting a C# application written as a multithreaded Windows Service and trying to squeeze every bit of performance out of it. 1) Does the thread that an object is instantiated on...
17
by: fctk | last post by:
some other doubts: 1) K&R, p50, 2.10: "if expr1 and expr2 are expressions, then expr1 op= expr2 is equivalent to expr1 = (expr1) op (expr2) except that expr1 is computed only once."
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
16
by: Haskell Prelude | last post by:
Hello Friends - Can anyone answer these C questions? 1. What is the effect of making an internal (local) variable static? 2. What is the effect of making an external (non-local) variable...
5
by: pantagruel | last post by:
Hi, It is generally stated that stringbuilder should be used instead of just concatenating strings with the plus operator. That's fine enough what I'm wondering in cases I have: String S =...
8
by: tfelb | last post by:
Hey group! I have 2 questions. I saw functions with char *dst = (char *)src. In that case if I remember what I've learned I assign (an) (the) address of src to dst. Right? But I can assign...
35
by: Lew Pitcher | last post by:
On November 14, 2008 15:00, in comp.lang.c, Nomen Nescio (nobody@dizum.com) wrote: Overkill Try
0
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,...
0
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$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...

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.