RE: Performance Issue with ForEach loop
- From: "Marek Suchenek" <MarekSuchenek@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 3 Nov 2005 12:12:06 -0800
MS needs to fix it.
My guess is that whoever implemented the code that is used in processing of
list-type structures followed the watered-down LIST scheme without current
position. Then the access time to n-th element is always Big Theta(n) even if
the elements are accessed conscutively!
In particular, processing the entire structure takes Big Theta (n*n) time!
So, processing 100 element list will take ~ 100*100 = 10,000 steps, while
processing it in 10 pieces 10 elements each will take only ~ 10*10*10 = 1,000
steps - a 10-fold improvement.
That's the price we all pay for watering down of CS curricula (and
textbooks). Those who graduated recently may not know how to do this kind of
things efficiently. For instance, over last 15 years I was teaching Data
Structures upper division or graduate courses, I noticed that elegant and
efficient implemantation of LIST (e.g. the one outlined in Aho, Hopcroft,
Ullman classic text) slowly disappeared.
Here is a much worse example:
ProgressBar1.Value=0
ProgressBar.Maximum = n
for i = 1 to n
ProgressBar1.Value.Increment()
Next
will show how the progress is slowing down as i grows (if n is big enough,
like, say 10,000), while it should be purely linear if the Increment() method
was implemented efficiently. Interestingly, the higher the ProgressBar1, the
more slow down one can observe.
I am betting $20 that people who implemented the mentioned above pieces for
MS are recent graduates who were not taught how to implement LIST-type
structures efficiently.
Marek Suchenek
"AK" wrote:
> Hi Everyone,
>
> For Last a few weeks I have been involved with performance tuning of an
> application. I have observed that if I do a loop over a collection
> (ArrayList/HashTable) which has 50000 objects in one shot then it takes about
> 15 minutes to do the computation. But if I break this to loop of only 5000
> objects and do all computation in 10 loops of 5000 each then it takes about 1
> minutes 30 seconds.
>
> I am trying to find a reason for this but I haven't been able to get one. If
> I do a search on net then it becomes more confusing as some will say don't
> use ForEach while some would say use only ForEach. I don't know which advice
> to take.
>
> (I have tried using For loop also but result is some what the same).
>
> The loop code look like:
> foreach(MyClass obj in listcollection)
> {
> // do something with this obj.
> }
>
>
> Can someone throw some light on the possible reasons for above mentioned
> observations.
>
> Thanks in advance,
> AMar.
.
- Follow-Ups:
- RE: Performance Issue with ForEach loop
- From: Jon Skeet [C# MVP]
- Re: Performance Issue with ForEach loop
- From: Alvin Bruney - ASP.NET MVP
- RE: Performance Issue with ForEach loop
- Prev by Date: RE: Threads: waiting and context switches
- Next by Date: Re: What's a good % Increase in performance
- Previous by thread: Re: Using Structure vs Class for Datasource
- Next by thread: Re: Performance Issue with ForEach loop
- Index(es):
Relevant Pages
|