• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

hoverfly

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

一个很基本也很常见的问题,转换string为int,几种方式的效率比较

比较转换string为int几种方式的效率
测试结果引子sitepoint。
方法1
try
{
   Convert.ToInt16(
"testing");
}
catch
{
}
方法2
string regexPattern = @"^(\d)+$";
System.Text.RegularExpressions.Regex checkInt 
= new System.Text.RegularExpressions.Regex(regexPattern);
return checkInt.IsMatch("TESTING");
方法3
double d = 0;
System.Globalization.CultureInfo myCulture 
= new System.Globalization.CultureInfo("en-GB");
Double.TryParse(
"testing",System.Globalization.NumberStyles.Integer,myCulture,out d);
其中方法3是.net 2.0引入的。
测试循环1000次,结果为方法1-10秒,方法2-0.3秒,方法3-0.2秒。
效率差异非常巨大。

posted on 2006-06-12 12:11  hoverfly  阅读(327)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3