4 questions about object declaration and its performance

G

Guest

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
 
G

Guest

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
 
A

Arne Janning

Unruled Boy said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top