Tips & Ticks by SAJJAD
C# speed: ‘return’ vs ‘yield return’
Declare;
public static T[] Reserve< T>(T[] array)
{
T[] newArray = new T[array.Length];
for (i = 0; i < array.Length; i++)
newArray[i] = array[array.Length - 1 - i];
return newArray;
}
public static IEnumerable ReserveAsync< T>(T[] array)
{
for (i = 0; i < array.Length; i++)
yield return array[array.Length - 1 - i];
}
Use;
int[] ii = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
long a;
a = DateTime.Now.Ticks;
foreach (int i in xArray.Reserve(ii))
Debug.WriteLine(i);
Debug.WriteLine(DateTime.Now.Ticks - a);
a = DateTime.Now.Ticks;
foreach (int i in xArray.ReserveAsync(ii))
Debug.WriteLine(i);
Debug.WriteLine(DateTime.Now.Ticks - a);
Result;
ReserveAsync often is 60% faster than Reserve
| Print article | This entry was posted by SAJJAD on October 7, 2009 at 9:43 pm, and is filed under C#, Language. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |