"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:11******** *************@g 44g2000cwa.goog legroups.com...
| Willy Denoyette [MVP] wrote:
| > | Yes, but I think that counts as cheating :)
| >
| > Yes, a little bit ;-), but isn't that (don't keep allocating
enumerators)
| > what ForEach is doing also?
|
| Well, there's a big difference between "copy this potentially huge
| array" and "allocate a single enumerator".
|
Not sure what you mean by that, ForEach doesn't allocate enumerators, while
foreach on the list allocates an enumerator and calls Dispose once per
iteration. 'foreach' on an array (as I did) doesn't allocate an enumerator,
it simply uses a pointer calls get_Length to get the length of the string
and advances the pointer.
| > | > Also, try to run the first two tests only, and watch the results....
| > |
| > | I don't see any difference there. What difference are you seeing?
| >
| > First version: 00:00:10.590905 9 (correct? True)
| > Second version: 00:00:04.496689 1 (correct? True)
| > Third version: 00:00:04.494429 5 (correct? True)
| >
| > First version: 00:00:11.449503 4 (correct? True)
| > Second version: 00:00:08.977291 5 (correct? True)
|
| Odd. How are you compiling and running? Do you get the same difference
| if you use the new benchmark as linked to from my blog entry?
| (
http://msmvps.com/blogs/jon.skeet/ar...reachperf.aspx)
|
| Jon
|
Compiling and running from the command line with /o and /o-. Using XP SP2
and 2.0.50727.42.
There is definitely something wrong with this (First version and Second
version), the problem doesn't occur when Third follows First. More trying to
profile the "second" version alone crashes the profilers (VS2005 as well as
special profiler using the HW CPU and memory controller perfcounters). I'm
actually looking into this to see what's happening.
The new version doesn't have any of these issues, that is, they show
consistent timing and no profiler crashes !?
Here are my results running on the same box as above results.
Compiled from the cmd line with /o
Test parameters: Size=100000; Iterations=1000 0
Test 00:00:11.021892 3: LanguageForEach
Test 00:00:04.541090 5: NewDelegateEach Time
Test 00:00:04.513824 8: CachedDelegate
Test 00:00:13.687767 0: LanguageForEach WithCopy1
Test 00:00:01.678258 2: LanguageForEach WithCopy2
Same with /o-
Test parameters: Size=100000; Iterations=1000 0
Test 00:00:11.665539 3: LanguageForEach
Test 00:00:04.582094 0: NewDelegateEach Time
Test 00:00:04.545938 6: CachedDelegate
Test 00:00:14.416172 1: LanguageForEach WithCopy1
Test 00:00:02.268416 2: LanguageForEach WithCopy2
First two tests only.
Test parameters: Size=100000; Iterations=1000 0
Test 00:00:11.048661 9: LanguageForEach
Test 00:00:04.538974 6: NewDelegateEach Time
Notice the huge difference for the last test when o is turned off! The fun
of micro-benchmarks.
Willy.