Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 229
  • Last Modified:

Singleton pattern in loop

Hi there,

I am processing a loop with around 1 million records i.e looping around 1 million times. I am creating number of objects within loop. I do NOT want to declare local variables outside the loop, but want to get maximum performance and minimum memory.

So, I am thinking of creating a Singleton CLASS which will hold all the variables/objects that needs to be declared and initialited within loop each time. Now, I will implement Singleton pattern on this class. In other words, Singleton object will be acquired for each run of the loop. Is this a valid option using which I can get maximum performance and minimum memory.
0
tech1guy
Asked:
tech1guy
  • 2
1 Solution
 
iUsernameCommented:
Sounds pretty good to me.
I also wouldn't call "get_instance()" function inside the loop to get the singleton pointer, but I would declare a variable that will hold a pointer to that singleton object, and call get_instance() only once before the loop.
0
 
tech1guyAuthor Commented:
but I need to RESET the variables/objects within the Singleton class for each loop run.
0
 
iUsernameCommented:
hmm, well, I don't really know what it means to "reset" the object in the singleton object.

In some cases it is better to simply create a new object (and dispose the last one), in other cases it is better to "reset' an object. depends on the object itself.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now