What is the major differences between Convert.ChangeType or Convert.ToInt32?
Is there any performance benefits of one ove开发者_高级运维r another among Convert.ChangeType or Convert.ToInt32 or int.Parse
If you know you're going to be converting a string
to Int32
, using Convert.ChangeType
seems an obscure way of doing that. I would definitely prefer either of the other calls to that.
The main difference between int.Parse
and Convert.ToInt32(x)
is that Convert.ToInt32(null)
returns 0 where as int.Parse(null)
will throw an exception. Of course, int.Parse
also gives you more control in terms of what culture is used.
I very much doubt that there's any performance benefit of one over the other: I would expect Convert.ToInt32
to call int.Parse
rather than the other way round - but it's not documented to work that way, and the hit of a single method call is unlikely to be significant. (It may well be inlined anyway.)
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 results this
266 on CalcChangeTypePerf
167 on CalcToInt32Perf
165 on CalcIntParse
Yes.
Convert.ToInt32 is better than using Convert.ChangeType for same purpose.
ChangeType is a general-purpose conversion method that converts the object specified by value to conversionType. While ToInt32 is specific for int32 type.
Simple test shows Parse()
is fastest method, next Convert.ToInt32()
and 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 is:
ChangeType=2422
ToInt32=1859
Parse=1760
ChangeType=2374
ToInt32=1857
Parse=1762
ChangeType=2378
ToInt32=1860
Parse=1763
ChangeType=2375
ToInt32=1855
Parse=1759
精彩评论