Re: Holding Returned Data in Arrays
From: Simon Sheffield (sisheffield_at_hotmail.com)
Date: 05/13/04
- Next message: Mattias Sjögren: "Re: Selecting the correct function using Type.GetMethod"
- Previous message: Niki Estner: "Re: Holding Returned Data in Arrays"
- In reply to: Alric: "Holding Returned Data in Arrays"
- Next in thread: Alric: "Re: Holding Returned Data in Arrays"
- Reply: Alric: "Re: Holding Returned Data in Arrays"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 13 May 2004 17:43:39 +0200
Alric,
The decision of whether or not that method is called per loop iteration is
actually the decision of the compiler, not the CLR.
In the case of C# + VB.NET (and most languages that support foreach i should
think), the results of the method call will be stored and then interated
over (you can see this if you ildasm / decompile the code below).
Also, whether or not new items are available whilst you loop depends on the
implementation of the thing you are iterating. Normally with a foreach loop
you are actually just repeatedly calling enumerator.MoveNext() until it
returns false. Whether or not 'new' items are returned depends on the
implementation of the enumerator.
In the case of Getfiles() you can see that it returns an array. Obviously an
array cannot 'grow' and so you are bound never to see 'new' items
when iterating over an array. If GetFiles() had returned an IEnumerable
type (that was not an array) then you could have expected to see new items
as they became available.
Interestingly, the C# + VB.NET compilers produce different output for
foreach loops depending on whether the term is an array or just IEnumerable.
For an array it actually expands directly into a for loop (thus also giving
you the CLR optimisation for array bounds checking). Fo all other
IEnumerables (or if you cast your array to IEnumerable first) it prodices a
while loop that repeatedly calls MoveNext(), as described above.
hope this helps
Simon
P.S. If you have a method call in a for loop, such as for ( int i=0;
i<dir.GetFiles().Length; i++ ), the C# compiler WILL call that method each
iteration.
"Alric" <anonymous@discussions.microsoft.com> wrote in message
news:B5E1DC96-D6B6-48BF-9591-0948B294ACC6@microsoft.com...
> I have tried to find an answer to this question, but I don't think I know
the correct search terms.
>
> When is it appropriate to cache the data returned from a function in a
holding variable? I am curious regarding what is really happening under the
.net hood.
>
> For example, let's look at looping through files in a directory.
>
> I could loop through the output of the GetFiles() function:
> >
> Dim loopFileInfo As FileInfo
> Dim dir As New DirectoryInfo("C:\Windows")
>
> For Each loopFileInfo In dir.GetFiles()
> Debug.WriteLine(loopFileInfo.FullName)
> Next
> >
>
> Or I could hold that output in an array and loop through that collection:
> >
> Dim loopFileInfo As FileInfo
> Dim fileInfoArray() As FileInfo
> Dim dir As New DirectoryInfo("C:\Windows")
>
> fileInfoArray = dir.GetFiles()
>
> For Each loopFileInfo In fileInfoArray
> Debug.WriteLine(loopFileInfo.FullName)
> Next
> >
>
> Will dir.GetFiles() be called more than once if I loop directly through
it? I always thought GetFiles() would be called once and then the output
would be stored in memory while the For Each loop processed, but some
reading lately has challenged this understanding.
>
> Obviously my question is not focused on this example. I'm trying to
understand what the general pattern is for the CLR. Is there generally a
performance benefit by caching collection return values before iterating
through them? Or are you just creating a second copy of the data for no
reason?
>
> In some basic testing of this GetFiles() example, no performance benefit
was seen by creating the holding collection. Also, my original
understanding -- that the function would only be called once -- has been
validated. If I starting iterating through GetFiles() and then add a few
files to the directory, the files are not magically added to the GetFiles()
output.
- Next message: Mattias Sjögren: "Re: Selecting the correct function using Type.GetMethod"
- Previous message: Niki Estner: "Re: Holding Returned Data in Arrays"
- In reply to: Alric: "Holding Returned Data in Arrays"
- Next in thread: Alric: "Re: Holding Returned Data in Arrays"
- Reply: Alric: "Re: Holding Returned Data in Arrays"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|