views:

1786

answers:

5

Is there any significant advantages for converting a string to an integer value between int.Parse() and Convert.ToInt32() ?

string stringInt = "01234";

int iParse = int.Parse(stringInt);

int iConvert = Convert.ToInt32(stringInt);

I found a question asking about casting vs Convert but I think this is different, right?

+1  A: 

The difference lies in the way both handles NULL value.

When encountered a NULL Value, Convert.ToInt32 returns a value 0. On other hand,Parse is more sensitive and expects a valid value. So it would throw an exception when you pass in a NULL.

Rulas
A: 

See this discussion for details.

Convert.ToInt32 won't throw as often (if stringInt == null, it returns 0 instead of throwing an exception), but has a slight bit more overhead since it's doing a few extra checks, then calling int.Parse internally.

Reed Copsey
+9  A: 

When passed a string as a parameter, Convert.ToInt32 calls int.Parse internally. So the only difference is an additional null check.

Here's the code from .NET Reflector

public static int ToInt32(string value)
{
    if (value == null)
    {
        return 0;
    }
    return int.Parse(value, CultureInfo.CurrentCulture);
}
Rob Windsor
The question asks for a "Performance" differences. Will that "null check" incur any performance penalties?
Sung Meister
Sung: Of course. A null check is another operation that has to be done. While it may be extremely small, there will be a performance penalty.
configurator
A: 

For what its worth:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int iterations = 1000000;
            string val = "01234";

            Console.Write("Run 1: int.Parse() ");
            DateTime start = DateTime.Now;
            DoParse(iterations, val);
            TimeSpan duration = DateTime.Now - start;
            Console.WriteLine("Duration: " + duration.TotalMilliseconds.ToString() + "ms");

            Console.Write("Run 1: Convert.ToInt32() ");
            start = DateTime.Now;
            DoConvert(iterations, val);
            duration = DateTime.Now - start;
            Console.WriteLine("Duration: " + duration.TotalMilliseconds.ToString() + "ms");

            Console.Write("Run 2: int.Parse() ");
            start = DateTime.Now;
            DoParse(iterations, val);
            duration = DateTime.Now - start;
            Console.WriteLine("Duration: " + duration.TotalMilliseconds.ToString() + "ms");

            Console.Write("Run 2: Convert.ToInt32() ");
            start = DateTime.Now;
            DoConvert(iterations, val);
            duration = DateTime.Now - start;
            Console.WriteLine("Duration: " + duration.TotalMilliseconds.ToString() + "ms");

            Console.Write("Run 3: int.Parse() ");
            start = DateTime.Now;
            DoParse(iterations, val);
            duration = DateTime.Now - start;
            Console.WriteLine("Duration: " + duration.TotalMilliseconds.ToString() + "ms");

            Console.Write("Run 3: Convert.ToInt32() ");
            start = DateTime.Now;
            DoConvert(iterations, val);
            duration = DateTime.Now - start;
            Console.WriteLine("Duration: " + duration.TotalMilliseconds.ToString() + "ms");

            Console.ReadKey();
        }

        static void DoParse(int iterations, string val)
        {
            int x;
            for (int i = 0; i < iterations; i++)
            {
                x = int.Parse(val);
            }
        }

        static void DoConvert(int iterations, string val)
        {
            int x;
            for (int i = 0; i < iterations; i++)
            {
                x = Convert.ToInt32(val);
            }
        }

    }
}

Result of 1,000,000 iterations of each:

Run 1: int.Parse() Duration: 312.5ms
Run 1: Convert.ToInt32() Duration: 328.125ms
Run 2: int.Parse() Duration: 296.875ms
Run 2: Convert.ToInt32() Duration: 312.5ms
Run 3: int.Parse() Duration: 312.5ms
Run 3: Convert.ToInt32() Duration: 312.5ms
rally25rs
+1  A: 

I wrote the code below and the result was that int.parse is slower than convert.toint32.

    static void Main(string[] args) {
        Console.WriteLine(TimeConvertTo());
        Console.WriteLine(TimeParse());
    }

    static TimeSpan TimeConvertTo() {
        TimeSpan start = DateTime.Now.TimeOfDay;
        for (int i = 0; i < 99999999; i++) {
            Convert.ToInt32("01234");
        }
        return DateTime.Now.TimeOfDay.Subtract(start);
    }

    static TimeSpan TimeParse() {
        TimeSpan start = DateTime.Now.TimeOfDay;
        for (int i = 0; i < 99999999; i++) {
            int.Parse("01234");
        }
        return DateTime.Now.TimeOfDay.Subtract(start);
    }