Re: Holding Returned Data in Arrays
From: Niki Estner (niki.estner_at_cube.net)
Date: 05/13/04
- Next message: Simon Sheffield: "Re: Holding Returned Data in Arrays"
- Previous message: Kian: "NetScheduleJobAdd task name"
- In reply to: Alric: "Holding Returned Data in Arrays"
- Next in thread: Simon Sheffield: "Re: Holding Returned Data in Arrays"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 13 May 2004 17:28:49 +0200
According to the docs foreach uses the IEnumerable/IEnumerator interface
which does not provide random-access, so it will have to store the reference
you give it.
I'm not sure if this answer is general enough for you, it applies to all
foreach loops.
It it however generally safe to make a copy of a collection object: they are
usually implemented as reference types, so your sample line:
> fileInfoArray = dir.GetFiles()
will not copy the data in the collection, it will only create another
reference to it - so you may always do this if in doubt.
Niki
"Alric" <anonymous@discussions.microsoft.com> wrote in
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: Simon Sheffield: "Re: Holding Returned Data in Arrays"
- Previous message: Kian: "NetScheduleJobAdd task name"
- In reply to: Alric: "Holding Returned Data in Arrays"
- Next in thread: Simon Sheffield: "Re: Holding Returned Data in Arrays"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|