C# int.Parse() 和 Convert.Toint() 之间有什么性能差异?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/641304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Any performance difference between int.Parse() and Convert.Toint()?
提问by CmdrTallen
Is there any significant advantages for converting a string to an integer value between int.Parse() and Convert.ToInt32() ?
在 int.Parse() 和 Convert.ToInt32() 之间将字符串转换为整数值有什么显着优势吗?
string stringInt = "01234";
int iParse = int.Parse(stringInt);
int iConvert = Convert.ToInt32(stringInt);
I found a questionasking about casting vs Convert but I think this is different, right?
我发现了一个问题,询问有关转换VS转换,但我认为这是不同的,对不对?
采纳答案by Rob Windsor
When passed a string as a parameter, Convert.ToInt32 calls int.Parse internally. So the only difference is an additional null check.
当将字符串作为参数传递时,Convert.ToInt32 在内部调用 int.Parse。所以唯一的区别是额外的空检查。
Here's the code from .NET Reflector
这是 .NET Reflector 的代码
public static int ToInt32(string value)
{
if (value == null)
{
return 0;
}
return int.Parse(value, CultureInfo.CurrentCulture);
}
回答by Rulas
The difference lies in the way both handles NULL value.
不同之处在于两者处理 NULL 值的方式。
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.
当遇到 NULL 值时,Convert.ToInt32 返回值 0。另一方面,Parse 更敏感并期望一个有效值。因此,当您传入 NULL 时,它会引发异常。
回答by Reed Copsey
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.
Convert.ToInt32 不会经常抛出(如果 stringInt == null,它返回 0 而不是抛出异常),但有一点点额外的开销,因为它做了一些额外的检查,然后在内部调用 int.Parse。
回答by CodingWithSpike
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:
每个 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
回答by CodingWithSpike
I wrote the code below and the result was that int.parse is slower than convert.toint32.
我写了下面的代码,结果是 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);
}
回答by CodingWithSpike
There are some performance implications as others have mentioned. If you look at the test code and performance stats from this website:
正如其他人提到的,有一些性能影响。如果您查看此网站的测试代码和性能统计信息:
- Int.Parse() and Int.TryParse() generally perform faster as the number of conversions you're performing increases.
- Convert.ToInt() seems to perform best with a low number of conversions
- The overall fastestway to convert a string to an int (assuming no exceptions) regardless of the number of conversions you need to perform is:
- 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');