Prince Ashitaka
Prince Ashitaka

Reputation: 8773

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

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

Upvotes: 7

Views: 4624

Answers (4)

acoolaum
acoolaum

Reputation: 2130

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

Upvotes: 0

Arsen Mkrtchyan
Arsen Mkrtchyan

Reputation: 50712

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

Upvotes: 5

Sunil Agarwal
Sunil Agarwal

Reputation: 4277

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.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500525

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

Upvotes: 9

Related Questions