What are the main differences between Convert.ChangeType or Convert.ToInt32?

Are there any performance advantages of one over the other among Convert.ChangeType or Convert.ToInt32 or int.Parse

+6
c #
source share
4 answers

If you know you are going to convert string to Int32 , using Convert.ChangeType seems like an obscure way to do this. I would definitely prefer any of the other calls.

The main difference between int.Parse and Convert.ToInt32(x) is that Convert.ToInt32(null) returns 0, where int.Parse(null) throws an exception. Of course, int.Parse also gives you more control in terms of which culture is used.

I doubt very much that anyone has a performance advantage: I would expect Convert.ToInt32 call int.Parse , and not vice versa, but it is not documented that way, and the hit of a method call is unlikely to be significant. (In any case, it can be embedded.)

+7
source share
 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 
+3
source share

Yes.

Convert.ToInt32 is better than using Convert.ChangeType for the same purpose.

ChangeType is a universal conversion method that converts an object specified by a value into a conversion type. Although ToInt32 is specific to the int32 type.

0
source share

A simple test shows Parse() the fastest method, the following Convert.ToInt32() and the last Convert.ChangeType() :

 static void Main(string[] args) { string s = "104563"; int a = 1; for (int k = 0; k < 4; k++) { Stopwatch w = Stopwatch.StartNew(); for (int i = 0; i < 10000000; i++) a = (int)Convert.ChangeType(s, typeof(int)); w.Stop(); Console.WriteLine("ChangeType={0}", w.ElapsedMilliseconds); Stopwatch w1 = Stopwatch.StartNew(); for (int i = 0; i < 10000000; i++) a = Convert.ToInt32(s); w1.Stop(); Console.WriteLine("ToInt32={0}", w1.ElapsedMilliseconds); Stopwatch w2 = Stopwatch.StartNew(); for (int i = 0; i < 10000000; i++) a = Int32.Parse(s); w2.Stop(); Console.WriteLine("Parse={0}", w2.ElapsedMilliseconds); } Console.ReadLine(); } 

Result:

 ChangeType=2422 ToInt32=1859 Parse=1760 ChangeType=2374 ToInt32=1857 Parse=1762 ChangeType=2378 ToInt32=1860 Parse=1763 ChangeType=2375 ToInt32=1855 Parse=1759 
0
source share

All Articles