RE: Performance Issue with ForEach loop

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Marek Suchenek <MarekSuchenek@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
> MS needs to fix it.

Fix what, exactly? What is the reproducible problem with foreach which
you believe has been identified?

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

That's a bad guess, and one which isn't borne out by evidence
elsewhere, or indeed the following program:

using System;
using System.Collections;

public class Test
{
const int Size = 100000000;

static void Main(string[] args)
{
ArrayList list = new ArrayList(Size);
for (int i=0; i < Size; i++)
{
list.Add("x"); // Just an arbitrary reference
}

DateTime start = DateTime.Now;
DateTime end = DateTime.Now;
int totalLength = 0;
foreach (string x in list)
{
totalLength += x.Length;
}
Console.WriteLine ("Iterating took {0}", end-start);
Console.WriteLine ("Total length={0}", totalLength);
}
}

On my laptop, that iterates through all 100,000,000 elements in under a
second. If the total cost of the whole operation were O(n^2), then even
if a single iteration only took a single instruction when JITted, that
would still indicate over 10^16 instructions being performed in under a
second.

I don't know what the OP's problem actually was, but I don't believe it
has anything to do with the performance of foreach itself. Far more
likely is that the operation he was performing within each chunk
depended on the chunk size.

> That's the price we all pay for watering down of CS curricula (and
> textbooks).

In this case, this post is the price we pay for you making wild guesses
without doing any testing.

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

Did the testing of hypotheses before saying that a company "needs to
fix" a problem which hasn't been proven to exist also disappear?

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

Good job you didn't bet your money on the implementation itself, isn't
it?

--
Jon Skeet - <skeet@xxxxxxxxx>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
.



Relevant Pages

  • Re: algorithm to compare 3 array elements
    ... There are several syntax mistakes. ... I was trying to learn the forEach from the Mozilla site and ... seemed like the for loop was easier to compare the index. ... I did try to fix the syntax error above with this line and it didn't ...
    (comp.lang.javascript)
  • Re: fix a per script
    ... Please help me to fix it. ... for $rec ... foreach $val ) ...
    (comp.lang.perl.misc)
  • Re: Invalid Argument supplied for foreach()
    ... My question is how would you fix this if there are NO results, ... "Sorry we are unable to process your search request or we are unable ... You $products array is empty. ... That way you will avoid the foreach error occurring. ...
    (comp.lang.php)
  • Re: parse string CN= OU= O=
    ... Below is some code that will fix that, but it's a bit of a hack - hopefully, someone else will provide a better alternative: ... foreach ) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Programming is not as much fun/more fun than it used to be.
    ... > And then I get to fix it. ... and by definition you dont get much of that at school. ... methods - is considered essential by the fresh graduate. ...
    (comp.lang.java.programmer)