Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the major differences between Convert.ChangeType or Convert.ToInt32?

Tags:

c#

Is there any performance benefits of one over another among Convert.ChangeType or Convert.ToInt32 or int.Parse

like image 925
Prince Ashitaka Avatar asked Jan 21 '11 07:01

Prince Ashitaka


People also ask

What is the difference between convert ToInt32 and convert toint64?

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).

What is the difference between convert ToInt32 and int parse?

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.

Which statement accurately describes the difference between Int32 TryParse () and convert ToInt32 ()?

Parse() and Int32. TryParse() can only convert strings. Convert. ToInt32() can take any class that implements IConvertible .

What is ToInt32?

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.


2 Answers

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.)

like image 190
Jon Skeet Avatar answered Sep 20 '22 16:09

Jon Skeet


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
like image 34
Arsen Mkrtchyan Avatar answered Sep 19 '22 16:09

Arsen Mkrtchyan