int.Parse()和Convert.Toint()之间的任何性能差异?

时间:2009-03-13 02:53:16

标签: c# performance

将字符串转换为int.Parse()和Convert.ToInt32()之间的整数值有什么显着优势吗?

string stringInt = "01234";

int iParse = int.Parse(stringInt);

int iConvert = Convert.ToInt32(stringInt);

我发现question询问有关施法和转换的问题,但我认为这是不同的,对吧?

6 个答案:

答案 0 :(得分:28)

当传递一个字符串作为参数时,Convert.ToInt32会在内部调用int.Parse。所以唯一的区别是额外的空检查。

以下是.NET Reflector的代码

public static int ToInt32(string value)
{
    if (value == null)
    {
        return 0;
    }
    return int.Parse(value, CultureInfo.CurrentCulture);
}

答案 1 :(得分:13)

它的价值:

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);
            }
        }

    }
}

每个的1,000,000次迭代的结果:

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

答案 2 :(得分:1)

区别在于两者处理NULL值的方式。

当遇到NULL值时,Convert.ToInt32返回值0。 另一方面,Parse更敏感并且期望有效值。 因此,当您传入NULL时会抛出异常。

答案 3 :(得分:1)

请参阅this discussion for details.

Convert.ToInt32不会经常抛出(如果stringInt == null,它返回0而不是抛出异常),但由于它正在做一些额外的检查,然后在内部调用int.Parse,所以会有更多的开销。

答案 4 :(得分:1)

我编写了下面的代码,结果是int.parse比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);
    }

答案 5 :(得分:1)

其他人提到了一些性能影响。如果您查看测试代码和效果统计信息from this website

  • Int.Parse()和Int.TryParse()通常会在您执行的转化次数增加时执行得更快。
  • Convert.ToInt()似乎在转化次数较少的情况下表现最佳
  • 将字符串转换为int的整体最快方式(假设没有例外),无论您需要执行的转换次数如何:

_

y = 0; //the resulting number from the conversion
//loop through every char in the string, summing up the values for the final number
for (int i = 0; i < s[x].Length; i++) 
    y = y * 10 + (s[x][i] - '0');