Hi!
I Was interested which of them would preform the fastest.
So i coded this:
Results:Code:using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Diagnostics; namespace Speeding { class Program { static void Main(string[] args) { Program p1 = new Program(); p1.check1(97000); p1.check2(97000); p1.check3(97000); Console.ReadKey(); } private void check1(int Size) { Stopwatch sp = new Stopwatch(); sp.Start(); for (int i = 0; i <= Size; i++) { for (int j = 0; j <= Size; j++) { } } sp.Stop(); Console.WriteLine("check 1(normal for) {1} cycles: {0}", sp.Elapsed, Size); } private void check2(int Size) { Stopwatch sp = new Stopwatch(); sp.Start(); int[] ii = new int[Size]; int[] jj = new int[Size]; foreach (int i in ii) { foreach (int j in jj) { } } sp.Stop(); Console.WriteLine("check 2(normal foreach) {1} cycles: {0}", sp.Elapsed, Size); } private void check3(int Size) { Stopwatch sp = new Stopwatch(); sp.Start(); int i = 0; int j = 0; while (i <= Size) { i++; while (j <= Size) { j++; } } sp.Stop(); Console.WriteLine("check 3(normal while) {1} cycles: {0}", sp.Elapsed, Size); } } }
Have i done something wrong????? because i am stunned by the results actually O.O





Reply With Quote