Penalty for instantiating an object tens of thousands of times?

G

Gary Frank

What are the ramifications if I were to instantiate an object tens of
thousands of times and add them to an array? Or hundreds of thousands of
times? Do you know if the act of instantiating a class takes a lot of
storage or other resources? Would it be a severe performance penalty?





From the .Net help doc:

You declare and use arrays of an object type just as you would declare and
use an array of any data type. The members of this array can be retrieved by
their index, and can be manipulated as any object of this type would be.
Arrays also have built-in functionality for searching and sorting that can
be accessed through the array variable. For more information on these
methods, see Array Class.



To create an array of objects



Declare the array as shown in the sample code below. Because arrays are
zero-based, they contain one member more than the upper bound you declare.

Dim x(10) As Widget ' Contains 11 members, from x(0) to x(10).

Instantiate each member of the array, or assign each member a reference to
an already existing object. An example of each approach is shown below:

' Instantiates each member of an array by using a loop.

Dim q As Integer

For q = 0 to 10

x(q) = New Widget()

Next



' Assigns a member of an array a reference to an existing object.

Dim myWidget As New Widget()

x(0) = myWidget

x(1) = myWidget

Note that you can assign the different members of the array references to
the same object.
 
O

One Handed Man \( OHM - Terry Burns \)

Dont forget you are storing references to these objects in the array. The
main penalty is use of memory but if there is enough of it then you should
be ok. I suppose it might give the Garbage collector something to chew on.
I would suggest you write a loop to generate it and see what happens.


--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
N

Nak

Hi there,

I wouldn't worry if I were you, I made a 3D engine which opened obj
files, this instantiated thousands upon thousands of objects. The only
speed pentalty was drawing them using GDI+. But one thing you might find
benificial is making typed collections, this way you can have the benefit of
a collection object rather that having to handle arrays on your own.

You can check out my 3D engine, it's not amazing trust me, but it is
capabale of creating allot of objects!

http://www.members.lycos.co.uk/nickpatemanpwp/

Go to "My own software > Starfield +", you'll notice that the engine is
called crapEngine, but it isn't that bad honest! :)

Nick.
 
J

Jared

Clever spam filter!

One Handed Man ( OHM - Terry Burns ) said:
Dont forget you are storing references to these objects in the array. The
main penalty is use of memory but if there is enough of it then you should
be ok. I suppose it might give the Garbage collector something to chew
on. I would suggest you write a loop to generate it and see what happens.


--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
G

Guest

I read you note and was curious as to how to make a typed collection? Do you
have any code examples or references?
 
L

Larry Serflaten

Gary Frank said:
What are the ramifications if I were to instantiate an object tens of
thousands of times and add them to an array? Or hundreds of thousands of
times? Do you know if the act of instantiating a class takes a lot of
storage or other resources? Would it be a severe performance penalty?

Creating new objects require memory, where the amount required depends
on the size of the object you are creating.. Under normal conditions the CLR
(runtime) is looking right at a large free memory area so allocations go real
quick, until you run out of memory.

HTH
LFS
 
O

One Handed Man \( OHM - Terry Burns \)

:)

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
N

Nak

Hi Dennis,

Here is an example of a collection of rectangle objects,

---------------------------------------------------------------------

Public Class rectangle_Collection
Inherits CollectionBase

#Region "Property interface"

Public Shadows Property item(ByVal iIndex As Integer) As Rectangle
Get
If ((Count = 0) OrElse ((iIndex < 0) And (iIndex > (Count -
1)))) Then Return (Nothing)
Return (CType(innerlist.Item(iIndex), Rectangle))
End Get
Set(ByVal Value As Rectangle)
If ((Count = 0) OrElse ((iIndex < 0) And (iIndex > (Count -
1)))) Then Return
innerlist.Item(iIndex) = Value
End Set
End Property

#End Region

#Region "Public methods"

Public Function add(ByVal iRectangle As Rectangle) As Integer
Return innerlist.Add(iRectangle)
End Function

Public Sub addRange(ByVal iRectangle() As Rectangle)
Call innerlist.AddRange(iRectangle)
End Sub

Public Sub addRange(ByVal iRectangleCollection As rectangle_Collection)
Call innerlist.AddRange(iRectangleCollection)
End Sub

Public Function contains(ByVal iRectangle As Rectangle) As Boolean
Return (innerlist.Contains(iRectangle))
End Function

Public Function indexOf(ByVal iRectangle As Rectangle) As Integer
Return (innerlist.IndexOf(iRectangle))
End Function

Public Sub insert(ByVal iIndex As Integer, ByVal iRectangle As
Rectangle)
Call innerlist.Insert(iIndex, iRectangle)
End Sub

Public Sub insertRange(ByVal iIndex As Integer, ByVal iRectangle() As
Rectangle)
Call innerlist.InsertRange(iIndex, iRectangle)
End Sub

Public Sub insertRange(ByVal iIndex As Integer, ByVal
iRectangleCollection As rectangle_Collection)
Call innerlist.InsertRange(iIndex, iRectangleCollection)
End Sub

Public Function lastIndexOf(ByVal iRectangle As Rectangle) As Integer
Return (innerlist.LastIndexOf(iRectangle))
End Function

Public Sub remove(ByVal iRectangle As Rectangle)
Call innerlist.Remove(iRectangle)
End Sub

#End Region

End Class

---------------------------------------------------------------------

Because it eliminates late binding you end up with a much faster
solution. It's also far more convenient than having to convert from an
object every time you wish to access any members. I use typed collections
all the time, every time it is possible to use one in fact. I also believe
there is some kind of add-in for VB.NET that generates them for you too,
though where I saw it I can't quite remember!

Nick.
 
O

One Handed Man \( OHM - Terry Burns \)

Here is another example of such a collection.

|---------------------|
| ----------------- |
| | | |
| ----------------- |
| |
| ---- ------- |
| | | | | |
| ---- ------- |
----------------------

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
N

Nak

LOL! You could have been a bit more artistic, how about

/\ /\
|-----------------------|
| ---- ----- |
| | /\ | | /\ | |
| ---- _ _ ----- |
| |
| |
|-----------------------|
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
| |
------------------------

Though this could hardly be seen as a collection of rectangles, tis a
collection of shapes!

Nick.
 
O

One Handed Man \( OHM - Terry Burns \)

LOL

--
OHM ( Terry Burns ) * Use the following to email me *

Dim ch() As Char = "ufssz/cvsotAhsfbuTpmvujpotXjui/OFU".ToCharArray()
For i As Int32 = 0 To ch.Length - 1
ch(i) = Convert.ToChar(Convert.ToInt16(ch(i)) - 1)
Next
Process.Start("mailto:" & New String(ch))
 
G

Guest

haven't tried it myself but apparently if you write
do
dim n as new myobject()
loop

then it will run forever and never crash, because the garbage collector will
keep freeing the ones that have gone out of scope.
 

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