Re: using foreach Vs for

From: Alex Feinman [MVP] (public_news_at_alexfeinman.com)
Date: 08/30/04


Date: Mon, 30 Aug 2004 09:23:09 -0700

It's not that foreach is faster. The for loop that uses indexer, which is
implemented via Item property, for which the accessor get_Item works via
PInvoke. It looks like PInvoke perf penalty is high enough to offset the
results

-- 
Alex Feinman
---
Visit http://www.opennetcf.org
"Ofer B." <ofer_berkovich@hotmail.com> wrote in message 
news:Oj4Lm1mjEHA.3896@TK2MSFTNGP15.phx.gbl...
> Thanks Alex.
>
> I understand the difference between the simple array and the ArrayList.
> but why is the foreach loop is faster than the for loop for the simple 
> array
> (Example 1)?
> I thought that a for loop is always faster than foreach, or is it
> because "m_byte = b;" is faster than "m_byte = m_array[i];"?
>
> Ofer
>
>
>
>
> "Alex Feinman [MVP]" <public_news@alexfeinman.com> wrote in message
> news:uiY3rPmjEHA.156@TK2MSFTNGP10.phx.gbl...
>> I think you'll find your answer in ildasm. In case of arrayList list
>> iterator has probably some overhead compared to indexed access.
>>
>> Looking in the implementation of IEnumerable and get_Item for both 
>> classes
>> we see that:
>> For Array the IEnumerator implementation is managed code that 
>> dereferences
>> next array element by incrementing the position inside the array, which
>> happens to be a simple increment by one in case of 1-dim array. The
> get_Item
>> accessor is implemented via P/Invoke, which probably is more efficient in
>> multidimensional array but the overhead of PInvoke makes it slower than
>> Enumerator
>>
>> For arrayList the implmentation of IEnumerator::MoveNext seems to be more
>> complex (some extra runtime checks) than get_Item and hence slower.
>>
>> -- 
>> Alex Feinman
>> ---
>> Visit http://www.opennetcf.org
>> "Ofer B." <ofer_berkovich@hotmail.com> wrote in message
>> news:eIEvJhljEHA.632@TK2MSFTNGP12.phx.gbl...
>> > Hi all,
>> > I have 2 examples, when to use "for" and when "foreach".
>> > In "example 1" we are using a simple array and the foreach  loop the is
>> > faster than the for loop.
>> > In "example 2" we are using  ArrayList and the for loop is faster than
> the
>> > foreach loop.
>> >
>> > The "TestOptimize()" is faster than the "TestStandard()".
>> >
>> > I don't understand the reason for the difference between the 2
> examples...
>> >
>> > if you understand, please explane
>> >
>> > Thanks
>> > Ofer
>> >
>> > ****************** Example 1 ****************
>> > static byte m_byte;
>> > static byte[] m_array;
>> >
>> > /// <summary>
>> > /// This method uses iterator to go through
>> > /// the array's elements
>> > /// </summary>
>> > public static void TestOptimized()
>> > {
>> >    for (int i=0; i < 1000; i++)
>> >        foreach(byte b in m_array)
>> >            m_byte = b;
>> > }
>> > /// <summary>
>> > /// This method access the array's elements
>> > /// thorugh index accessor
>> > /// </summary>
>> > public static void TestStandard()
>> > {
>> >    for (int s = 0; s < 1000; s++)
>> >        for(int i =0; i < 100000; i++)
>> >            m_byte = m_array[i];
>> > }
>> > **************** Example 2 *****************************
>> > static object m_obj;
>> > static ArrayList m_array;
>> >
>> > /// <summary>
>> > /// This method uses iterator to go through
>> > /// the array's elements
>> > /// </summary>
>> > public static void TestStandard()
>> > {
>> >    for (int i=0; i < 1000; i++)
>> >        foreach(object b in m_array)
>> >            m_obj = b;
>> > }
>> > /// <summary>
>> > /// This method access the array's elements
>> > /// thorugh index accessor
>> > /// </summary>
>> > public static void TestOptimized()
>> > {
>> > for (int s = 0; s < 1000; s++)
>> >    for(int i =0; i < 10000; i++)
>> >        m_obj = m_array[i];
>> > }
>> > *************************************************
>> >
>> >
>>
>>
>
> 


Relevant Pages

  • Re: a question about for and foreach
    ... foreach (@array) { ... why @array are changed after this foreach loop!!! ... The "foreach" loop iterates over a normal list value and sets the ... If any element of LIST is an lvalue, you can modify it by modifying ...
    (perl.beginners)
  • Re: foreach statement output
    ... by using a 'for' statement instead of the foreach loop: ... and I will use the array to generate a string. ... know about foreach loop workings. ...
    (comp.infosystems.www.authoring.cgi)
  • Re: foreach statement output
    ... know about foreach loop workings. ... I've got an incoming array of unknown ... The Perl Cookbook ... My big confusion over this foreach issue is I failed to realize that the ...
    (comp.infosystems.www.authoring.cgi)
  • Re: Strange behaviour
    ... Personally, I would use the foreach version, as it is easier to read. ... bool AllNegative{ ... besides the difference between for and foreach the first example will always test each element of the array while the second example only tests until it finds the first non-negative. ... This is independant of for/foreach since also a for loop can be exited with return ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: safe to delete elements of array in foreach
    ... (normally this is not the case but not sure in php) ... I make it a habit not to delete entries in a foreach() loop. ... build an array of keys I want to delete, and after the loop ends, ...
    (comp.lang.php)