tryParse, try/catch(Parse), Convert比较
2010-01-31 13:41
176 查看
只是一个别人写的文章精简一下,原文在这里:http://blogs.msdn.com/ianhu/archive/2005/12/19/505702.aspx
话不多,直接上代码:
代码
private List<Int32> TestParse(String[] strings)
{
Int32 intValue;
List<Int32> intList = new List<int>();
for (int i = 0; i < 5000000; i++)
{
intList.Clear();
foreach (String str in strings)
{
try
{
intValue = Int32.Parse(str);
intList.Add(intValue);
}
catch (System.ArgumentException)
{ }
catch (System.FormatException)
{ }
catch (System.OverflowException)
{ }
}
}
return intList;
}
private List<Int32> TestTryParse(String[] strings)
{
Int32 intValue;
List<Int32> intList = new List<int>();
Boolean ret;
for (int i = 0; i < 5000000; i++)
{
intList.Clear();
foreach (String str in strings)
{
ret = Int32.TryParse(str, out intValue);
if (ret)
{
intList.Add(intValue);
}
}
}
return intList;
}
private List<Int32> TestConvert(String[] strings)
{
Int32 intValue;
List<Int32> intList = new List<int>();
for (int i = 0; i < 5000000; i++)
{
intList.Clear();
foreach (String str in strings)
{
try
{
intValue = Convert.ToInt32(str);
intList.Add(intValue);
}
catch (System.FormatException)
{ }
catch (System.OverflowException)
{ }
}
}
return intList;
}
测试正确数据:
{ "123", "4567", "7890", "1", "1231280", "10" }.
三者性能差不多。
测试错误数据:
{ "12345", null, "123", "1324dfs", "51235", String.Empty, "43", "4123412341234123412341234123412341234123" }.
如图所见:tryParse>Convert>Parse
原因在于Convert跟Parse要进行异常处理,尤其是Parse的ArgumentNullException处理开销很大。
总结:
在没特殊需求的情况下强烈建议使用tryParse
话不多,直接上代码:
代码
private List<Int32> TestParse(String[] strings)
{
Int32 intValue;
List<Int32> intList = new List<int>();
for (int i = 0; i < 5000000; i++)
{
intList.Clear();
foreach (String str in strings)
{
try
{
intValue = Int32.Parse(str);
intList.Add(intValue);
}
catch (System.ArgumentException)
{ }
catch (System.FormatException)
{ }
catch (System.OverflowException)
{ }
}
}
return intList;
}
private List<Int32> TestTryParse(String[] strings)
{
Int32 intValue;
List<Int32> intList = new List<int>();
Boolean ret;
for (int i = 0; i < 5000000; i++)
{
intList.Clear();
foreach (String str in strings)
{
ret = Int32.TryParse(str, out intValue);
if (ret)
{
intList.Add(intValue);
}
}
}
return intList;
}
private List<Int32> TestConvert(String[] strings)
{
Int32 intValue;
List<Int32> intList = new List<int>();
for (int i = 0; i < 5000000; i++)
{
intList.Clear();
foreach (String str in strings)
{
try
{
intValue = Convert.ToInt32(str);
intList.Add(intValue);
}
catch (System.FormatException)
{ }
catch (System.OverflowException)
{ }
}
}
return intList;
}
测试正确数据:
{ "123", "4567", "7890", "1", "1231280", "10" }.
三者性能差不多。
测试错误数据:
{ "12345", null, "123", "1324dfs", "51235", String.Empty, "43", "4123412341234123412341234123412341234123" }.
如图所见:tryParse>Convert>Parse
原因在于Convert跟Parse要进行异常处理,尤其是Parse的ArgumentNullException处理开销很大。
总结:
在没特殊需求的情况下强烈建议使用tryParse
相关文章推荐
- 比较Parse,TryParse,Convert
- Parse,TryParse,Convert 比较
- C#中转换函数Convert、Parse、TryParse、(int) 的区别
- Convert、Parse、TryParse、(int)区别及可能引发性能问题
- Convert、Parse、TryParse、(int)等区别
- C#中(int)、int.Parse()、int.TryParse、Convert 区别
- Convert、Parse、TryParse、(int)
- Convert、Parse、TryParse、(int) 的区别
- Convert、Parse、TryParse、(int)等区别
- 数据类型转换的三种方式 Convert,parse和TryParse的解析
- 关于输出null对象,以及try/catch/finally比较好玩的一段代码
- C#(int)中Convert、Parse、TryParse的区别
- Convert、Parse、TryParse、(int) 区别
- Convert、Parse、TryParse、(int) 区别
- Convert、Parse、TryParse、(int) 的区别
- (转)Convert、Parse、TryParse、(int) 区别
- 02 C#(int)中Convert、Parse、TryParse的区别
- C#(int)中Convert、Parse、TryParse的区别
- Convert、Parse、TryParse、(int)等区别(细节小记)
- 网友面试题:C#,不使用任何Parse,TryParse,Convert方法,自写函数将数字字符串转换成整数