O
Ofer B.
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;
}
**************** 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 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;
}
**************** 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;
}
*************************************************