?
Solved

Is this good practice? - Create an object instance in a loop

Posted on 2005-03-05
10
Medium Priority
?
236 Views
Last Modified: 2010-04-16
Hello

I am relatively new to C#, but have done a bit of Java in the past. I can not remember however if this is good practice?

I have this code

foreach(string fileName in fileEntries)
{
ArrayList fileDetailsArrayList = new ArrayList(); ***************
                  
FileInfo fi = new FileInfo(fileName);
long fileSize = fi.Length;
                  
//put the name and size into an ArrayList
fileDetailsArrayList.Add(fileName);
fileDetailsArrayList.Add(fileSize);
                  
//add the fileDetailsArrayList to the global Arraylist
arrayList.Add(fileDetailsArrayList);
}

My question is concerning the line marked with the ***'s. Is this good practice? Every time that chunk of code loops it will create a new ArrayList called fileDetailsArrayList, which works, but something in my mind tells me i'm causing unnessaccary memory usage or something.

If its all good, and C# handles it ok, then thats fine :-)
0
Comment
Question by:pee_cee
[X]
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
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 14

Expert Comment

by:Thandava Vallepalli
ID: 13465214
No problem.  CLR (Runtime Engine) is will handle it.......
0
 
LVL 17

Accepted Solution

by:
Jesse Houwing earned 150 total points
ID: 13465236
It's better to do this:

ArrayList fileDetailsArrayList = null;
FileInfo fi = null;
long fileSize = 0;

foreach(string fileName in fileEntries)
{
fileDetailsArrayList = new ArrayList();
fi = new FileInfo(fileName);
fileSize = fi.Length;
               
//put the name and size into an ArrayList
fileDetailsArrayList.Add(fileName);
fileDetailsArrayList.Add(fileSize);
               
//add the fileDetailsArrayList to the global Arraylist
arrayList.Add(fileDetailsArrayList);
}

As this prevents pointer allocation on each.
0
 
LVL 10

Expert Comment

by:makerp
ID: 13466762
The code posted by ToAoM does not provide any significant performance gains, all it does is unneccesarily increase the scope of your variables. Stick with what you have, its more consise.

-P
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:makerp
ID: 13466766
>>As this prevents pointer allocation on each.

how is this so, each loop still causes new to be called. its just down to where in the code the variables are declared, in your case outside of the loop instead of inside. in anycase there are still 3 variables declared taking up exactly the same amount of space.
0
 
LVL 3

Author Comment

by:pee_cee
ID: 13466979
Ok, so really there was nothing wrong with my code?
If this is the case, i assume itsvtk gets the points?
0
 
LVL 17

Expert Comment

by:Jesse Houwing
ID: 13467151
There is nothing wrong with it, but it is common to declare your variables outside of a loop, so that they don't get declared and removed each time round. The performance normally is neglectable, but in some cases it can give you a major increase in speed, especially if the variables internally use a lot of value types.
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 13470882
I agree with ToAoM - declare it outside the loop because inside it CAN cause a performance loss.

But what I don't understand is why you would initialize the arraylist inside the loop anyway. Why not just:

ArrayList fileDetailsArrayList = new ArrayList();

foreach(string fileName in fileEntries)
{
             
FileInfo fi = new FileInfo(fileName);
long fileSize = fi.Length;
             
//put the name and size into an ArrayList
fileDetailsArrayList.Add(fileName);
fileDetailsArrayList.Add(fileSize);
             
//add the fileDetailsArrayList to the global Arraylist
arrayList.Add(fileDetailsArrayList);
}


----------------------------

This would be even better.
0
 
LVL 3

Author Comment

by:pee_cee
ID: 13474750
That would not work for me, as fileDetailsArrayList needs to be empty each time i enter the loop.
With that suggestion, i believe the fileDetailsArrayList will just grow in size.

I'm not really keen on this implementation anyway, so may be rethinking the design anyway!

I'll give points to ToAoM, but if anyone disagrees, i'm sure an admin can sort it out!
0
 
LVL 8

Expert Comment

by:Razzie_
ID: 13477704
Ok then what I don't understand is why you want to create an arraylist only to add 1 long and 1 string to the global arraylist? seems like overkill to me anyway and bad for performance! You should think about using a Hashtable instead and store a filename with its corresponding filesize as a key / value pair.

Regards,

Razzie
0
 
LVL 3

Author Comment

by:pee_cee
ID: 13479232
a-ha. sounds like a plan batman. i shall look into this.

basically, i want a list of lots of files, with their sizes.
i remember doing java hashtables many moons ago.....i shall investigate.

thanks for the help all.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

766 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