471,086 Members | 954 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Penalty for instantiating an object tens of thousands of times?

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.
Nov 21 '05 #1
6 1473
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))
--
"Gary Frank" <ga**************@REMOVEsbcglobal.net> wrote in message
news:bD*****************@newssvr11.news.prodigy.co m...
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.

Nov 21 '05 #2
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.

"Gary Frank" <ga**************@REMOVEsbcglobal.net> wrote in message
news:bD*****************@newssvr11.news.prodigy.co m...
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.

Nov 21 '05 #3
Clever spam filter!

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in message
news:Os**************@TK2MSFTNGP15.phx.gbl...
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))
--
"Gary Frank" <ga**************@REMOVEsbcglobal.net> wrote in message
news:bD*****************@newssvr11.news.prodigy.co m...
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.


Nov 21 '05 #4
I read you note and was curious as to how to make a typed collection? Do you
have any code examples or references?

"Nak" wrote:
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.

"Gary Frank" <ga**************@REMOVEsbcglobal.net> wrote in message
news:bD*****************@newssvr11.news.prodigy.co m...
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.


Nov 21 '05 #5

"Gary Frank" <ga**************@REMOVEsbcglobal.net> wrote
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

Nov 21 '05 #6
:)

--
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))
--
"Jared" <as***********@nospam.com> wrote in message
news:10*************@corp.supernews.com...
Clever spam filter!

"One Handed Man ( OHM - Terry Burns )" <news.microsoft.com> wrote in
message news:Os**************@TK2MSFTNGP15.phx.gbl...
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))
--
"Gary Frank" <ga**************@REMOVEsbcglobal.net> wrote in message
news:bD*****************@newssvr11.news.prodigy.co m...
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.



Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Roberto Gerola | last post: by
1 post views Thread by Peter Bär | last post: by
5 posts views Thread by Chris Spencer | last post: by
2 posts views Thread by Kevin Stone | last post: by
36 posts views Thread by zouyongbin | last post: by
3 posts views Thread by Berteun Damman | last post: by

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.