Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Singleton pattern in loop

Posted on 2009-07-13
3
218 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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