Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using new or global for objects in functions 1

Status
Not open for further replies.

laurin1

MIS
Apr 28, 2003
77
US
I've got some code with a bunch of functions that use the same object over and over again. Is it better (faster, more memory efficient) to create that object in each function or to create it globally and use the global keyword in each function?

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
I don't think your question can be answered with the information available.

It may or may not use more memory to create a new object each time. How many objects are in existence at once?

It may or may not be faster to use a single object. What are you doing to the object.

As a general rule, I reuse an object any time I need to maintain the data in it.



Want the best answers? Ask the best questions! TANSTAAFL!
 
The data in the object is not re-used from function to function. Since the object is created in each function and only used there, I think only 1 in existence at once (well, there are a couple of instances where I create two at once.) The object is simply a class used create <INPUT> elements on some very complex forms. It's not even used to store data, but rather to load data apply a method to that data, then load new data and apply a method to it, and so on.

Keith Davis
MCSA, A+, N+, Guru+, Geek+, Child of God++++++
Love and Service
 
Then there is no problem with memory, as only one copy of the object will exist at a time. It is probably theoretically faster to reuse a single object, but probably not by much.

I don't see anything wrong with what you're doing.



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top