Re: Best practice using large objects in foreach



I don't see any object reuse, at most variable reuse. Compiler does not care
much about it. Nor does CLR.
From the performance point of view, A & B are the same. C is slower because
of using().

"Benny" <bennyandlinds@xxxxxxxxx> ha scritto nel messaggio
news:1154011992.988169.191070@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I just wanted to throw the discussion out there on what the best
practice people feel is for using large objects in a foreach loop. For
example if you are reusing an Image object in a loop like this (letters
above snippets are for reference purposes):

A
foreach ( string s in myList )
{
Image img = Image.FromFile( s );
// operate on img
}

would it run more efficiently like this?

B
Image img = null;
foreach ( string s in myList )
{
img = Image.FromFile( s );
// operate on img
}

or

C
foreach ( string s in myList )
{
using ( Image img = Image.FromFile( s );
{
// operate on img
}
}

the list goes on....

Let me know what your thoughts are.



.



Relevant Pages

  • Best practice using large objects in foreach
    ... practice people feel is for using large objects in a foreach loop. ... foreach (string s in myList) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: System.AccessViolationException
    ... internal string theAP; ... DataColumn col = new DataColumn; ... private string _pwd; ... foreach ...
    (microsoft.public.dotnet.languages.csharp)
  • Access denied
    ... First it builds wsdl string: ... foreach ... CSharpCodeProvider cscp = new CSharpCodeProvider; ... CompilerParameters cp = new CompilerParameters; ...
    (microsoft.public.dotnet.languages.csharp)
  • Access denied
    ... My problem is that it doesn't work if Anonymous Access is turned off. ... First it builds wsdl string: ... foreach ... CompilerParameters cp = new CompilerParameters; ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Weird Error Msg
    ... foreach file $txtfile { ... set TestStr ... want to match a glob-style pattern, a regular expression, or a fixed string. ... lists, and the foreach expects it's second argument to be a list. ...
    (comp.lang.tcl)

Loading