C#的性能如何优化

这篇文章主要讲解了“C#的性能如何优化”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“C#的性能如何优化”吧!

成都创新互联公司主营中牟网站建设的网络公司,主营网站建设方案,app软件开发公司,中牟h5小程序开发搭建,中牟网站营销推广欢迎中牟等地区企业咨询

减少重复代码

这是最基本的优化方案,尽可能减少那些重复做的事,让他们只做一次,比较常见是这种代码,同样的Math.Cos(angle) 和Math.Sin(angle)都做了2次。

private Point RotatePt(double angle, Point pt)  {       Point pRet = new Point();       angle = -angle;       pRet.X = (int)((double)pt.X * Math.Cos(angle) - (double)pt.Y * Math.Sin(angle));       pRet.Y = (int)((double)pt.X * Math.Sin(angle) + (double)pt.Y * Math.Cos(angle));       return pRet;  }

优化后

private Point RotatePt3(double angle, Point pt)  {      Point pRet = new Point();      angle = -angle;      double SIN_ANGLE = Math.Sin(angle);      double COS_ANGLE = Math.Cos(angle);      pRet.X =(int)(pt.X * COS_ANGLE - pt.Y * SIN_ANGLE);      pRet.Y = (int)(pt.X * SIN_ANGLE + pt.Y * COS_ANGLE);      return pRet;  }

还有另一种 ,在方法中实例化一个对象, 但是这个对象其实是可以复用的。

public static string ConvertQuot(string html)  {      Regex regex = new Regex("&(quot|#34);", RegexOptions.IgnoreCase);      return regex.Replace(html, "\"");  }

优化后

readonly static Regex ReplaceQuot = new Regex("&(quot|#34);", RegexOptions.IgnoreCase | RegexOptions.Compiled);  public static string ConvertQuot(string html)  {      return ReplaceQuot.Replace(html, "\"");  }

还有一种是不必要的初始化,比如调用out参数之前,是不需要初始化的。

public bool Check(int userid)  {      var user = new User();      if(GetUser(userid,out user))      {          return user.Level > 1;      }      return false;  }

这里的new User()就是不必要的操作,

优化后

public bool Check(int userid)  {      User user;      if(GetUser(userid,out user))      {          return user.Level > 1;      }      return false;  }

不要迷信正则表达式

正好在***个栗子里说到了正在表达式(Regex)对象就顺便一起说了。

很多人以为正则表达式很快,非常快,超级的快。

虽然正则表达式是挺快的,不过千万不要迷信他,不信你看下面的栗子。

//方法1  public static string ConvertQuot1(string html)  {      return html.Replace(""", "\"").Replace(""", "\"");  }   readonly static Regex ReplaceQuot = new Regex("&(quot|#34);", RegexOptions.IgnoreCase | RegexOptions.Compiled);  //方法2  public static string ConvertQuot2(string html)  {      return ReplaceQuot.Replace(html, "\"");  }

有多少人认为正则表达式比较快的,举个手??

C#的性能如何优化

结果为10w次循环的时间 ,即使是10个Replace连用,也比Regex好,所以不要迷信他。

//方法1  public static string ConvertQuot1(string html)  {      return html.Replace("0", "").Replace("1", "").Replace("2", "").Replace("3", "").Replace("4", "").Replace("5", "").Replace("6", "").Replace("7", "").Replace("8", "").Replace("9", "");  }   readonly static Regex ReplaceQuot = new Regex("[1234567890]", RegexOptions.IgnoreCase | RegexOptions.Compiled);  //方法2  public static string ConvertQuot2(string html)  {      return ReplaceQuot.Replace(html, "");  }

ConvertQuot1:3518

ConvertQuot2:12479

***给你们看一个真实的,杯具的栗子。

Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", RegexOptions.IgnoreCase);  Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "", RegexOptions.IgnoreCase);  Htmlstring = Regex.Replace(Htmlstring, @"-->", "", RegexOptions.IgnoreCase);  Htmlstring = Regex.Replace(Htmlstring, @"