private const int maxValue = 1000000; static void Main(string[] args) { string[] strArray = new string[maxValue]; for (int i = 0; i < maxValue; i++) { strArray[i] = i.ToString(); } int[] parsedNums = new int[maxValue]; CalcChangeTypePerf(strArray,parsedNums); CalcToInt32Perf(strArray, parsedNums); CalcIntParse(strArray, parsedNums); } public static void CalcChangeTypePerf(string[] strArray,int[] parsedArray) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < maxValue; i++) { parsedArray[i] = (int)Convert.ChangeType(strArray[i], typeof(int)); } stopwatch.Stop(); Console.WriteLine("{0} on CalcChangeTypePerf", stopwatch.ElapsedMilliseconds); } public static void CalcToInt32Perf(string[] strArray, int[] parsedArray) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < maxValue; i++) { parsedArray[i] = Convert.ToInt32(strArray[i]); } stopwatch.Stop(); Console.WriteLine("{0} on CalcToInt32Perf", stopwatch.ElapsedMilliseconds); } public static void CalcIntParse(string[] strArray, int[] parsedArray) { Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); for (int i = 0; i < maxValue; i++) { parsedArray[i] = int.Parse(strArray[i]); } stopwatch.Stop(); Console.WriteLine("{0} on CalcIntParse", stopwatch.ElapsedMilliseconds); }
This simple test shows that it
266 on CalcChangeTypePerf 167 on CalcToInt32Perf 165 on CalcIntParse
Arsen mkrtchyan
source share