Is there any performance benefits of one over another among Convert.ChangeType or Convert.ToInt32 or int.Parse
Thus int32 would be limited to a value between (-256^4/2) and (256^4/2-1). And int64 would be limited to a value between (-256^8/2) and (256^8/2-1).
In brief, int. Parse and Convert ToInt32 are two methods to convert a string to an integer. The main difference between int Parse and Convert ToInt32 in C# is that passing a null value to int Parse will throw an ArgumentNullException while passing a null value to Convert ToInt32 will give zero.
Parse() and Int32. TryParse() can only convert strings. Convert. ToInt32() can take any class that implements IConvertible .
ToInt32(String, Int32) Converts the string representation of a number in a specified base to an equivalent 32-bit signed integer. ToInt32(UInt64) Converts the value of the specified 64-bit unsigned integer to an equivalent 32-bit signed integer.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With