Solved

Singleton pattern in loop

Posted on 2009-07-13
3
214 Views
Last Modified: 2012-05-07
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
Comment
Question by:tech1guy
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
iUsername earned 500 total points
ID: 24842169
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
 

Author Comment

by:tech1guy
ID: 24842306
but I need to RESET the variables/objects within the Singleton class for each loop run.
0
 
LVL 5

Expert Comment

by:iUsername
ID: 24842510
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Article by: Ivo
Anonymous Types in C# by Ivo Stoykov Anonymous Types are useful when  we do not need to follow usual work-flow -- creating object of some type, assign some read-only values and then doing something with them. Instead we can encapsulate this read…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now