RE: Performance Issue with ForEach loop

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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.
.



Relevant Pages

  • Re: Demodulating QPSK
    ... I have a general-purpose PLL implemented as a primitive in a language where I can easily run signals through it, plot the outputs, and tweak its parameters: initial frequency, loop gain, loop bandwidth and I/Q filter bandwidth. ... When I process File1 through the PLL and observe the control frequency being fed back to the VFO (this would be the locked frequency), I see a brief transient while the loop acquires lock. ...
    (comp.dsp)
  • Re: Discovery: dictionaries load slow unless you have the right key/value pair in the right format
    ... then later, inside of a massive (million iteration) loop, I had this: ... Rectangle type) and the key, ... Anybody observe this? ... is being created everytime in the loop, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Fastcode CharPosIEx B&V 1.0.2
    ... Now open the CPU view and observe that LStrLen is called once for every loop ... Index, LengthS: Integer; ... Jeg beskyttes af den gratis SPAMfighter til privatbrugere. ...
    (borland.public.delphi.language.basm)
  • Re: Discovery: dictionaries load slow unless you have the right key/value pair in the right format
    ... then later, inside of a massive (million iteration) loop, I had this: ... Rectangle type) and the key, ... Anybody observe this? ... is being created everytime in the loop, ...
    (microsoft.public.dotnet.languages.csharp)