欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

編程語言中可能引發(fā)性能問題的代碼寫法有哪些

這篇文章將為大家詳細(xì)講解有關(guān)編程語言中可能引發(fā)性能問題的代碼寫法有哪些,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

創(chuàng)新互聯(lián)建站是一家專業(yè)提供長壽企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)、H5頁面制作、小程序制作等業(yè)務(wù)。10年已為長壽眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。

1. int.Parse() VS int.TryParse()

你是不是正在這樣寫

int id = 0;  try{ id = int.Parse(Request["id"]); }  catch{ id = 0; }

如果是可以這樣試試

int id = 0;  int.TryParse(Request["id"], out id);

前一種寫法,一旦 Request["id"] 是非數(shù)值型的,將引發(fā)一個(gè)異常,引發(fā)異常的開銷是非常巨大的,而后一種則不會(huì)引發(fā)任何異常。

2.string.IndexOf()

你正在這樣寫嗎?

string s = "aaa,bb";  int pos = s.IndexOf(",");

其實(shí)對于單字符的查找,這樣寫會(huì)更好

string s = "aaa,bb";  int pos = s.IndexOf(',');

有人會(huì)問如果我要找多個(gè)字符呢,那可以試試下面的

string s = "aaa,bb";  int pos = s.IndexOf("bb", StringComparison.OrdinalIgnoreCase);

至于 StringComparison 的具體用法可以google或者baidu得到。

3. RegexOptions.Compiled

如果你正在使用正則并且用到了這個(gè)參數(shù),那請你慎重,根據(jù)個(gè)人經(jīng)驗(yàn)使用這個(gè)參數(shù)在訪問量比較大的情況下可能會(huì)引發(fā)性能問題,比如cpu偏高。如果你表示懷疑,可以嘗試比較使用和不用這個(gè)參數(shù)的情況下哪個(gè)性能會(huì)更好。

4.忘記關(guān)閉數(shù)據(jù)庫連接

數(shù)據(jù)庫連接是非常好資源的,所以從打開到關(guān)閉應(yīng)該越短越好。想看看有沒有忘記關(guān)閉,可以通過性能監(jiān)視器的 .net Data provider for SqlClient ( 假設(shè)你用的是sqlserver ) 來查看,具體的參數(shù)說明可以通過google和baidu得到。

5.頻繁的Response.Write()

你正在這樣做嗎?

Response.Write("這是第1行.< br/>");  Response.Write("這是第2行.< br/>");  Response.Write("這是第3行.< br/>");

此種寫法頻繁調(diào)用Response.Write() ,根據(jù)經(jīng)驗(yàn),這是相當(dāng)?shù)暮腸pu,改成下面的試試

StringBuilder sb = new StringBuilder();  sb.Append("這是第1行.< br/>");  sb.Append("這是第2行.< br/>");  sb.Append("這是第3行.< br/>");   Response.Write(sb.ToString());

6. 不必要的重復(fù)操作

List< TopicInfo> list = new List< TopicInfo>();   //從數(shù)據(jù)庫得到數(shù)據(jù)  list = GetDataFromDB();  for(int i = 0;i <  list.Count; i++ )  {      TopicInfo item = list[i];              }

上面的代碼估計(jì)誰都看的出來有什么不好,那下面這個(gè)代碼呢

using System;  using System.Collections.Generic;  using System.Text;   using System.Text.RegularExpressions;  using System.Web;   public class UrlUBB  {           /// < summary>          /// 替換UBB里面的url          /// < /summary>          /// < param name="content">< /param>          /// < returns>< /returns>          public static string RegularUrl(string content)          {              if (string.IsNullOrEmpty(content))                  return string.Empty;               if (content.IndexOf(@"(url=", StringComparison.OrdinalIgnoreCase) == -1 || content.IndexOf(@"(/url)", StringComparison.OrdinalIgnoreCase) == -1)                  return content;               Regex reg = new Regex(@"\(url=(?< url>.[^\)]*)\)(?< name>.[^\(]*)\(/url\)");               string url = string.Empty;              string name = string.Empty;              string href = string.Empty;               MatchCollection matches = reg.Matches(content);              foreach (Match m in matches)              {                  if (m.Success)                  {                      url = regexUrl(m.Groups["url"].ToString());                      name = m.Groups["name"].ToString();                      href = string.Format("< a href=\"redirect.aspx?goto={0}\">{1}< /a>", url, name);                      content = content.Replace(m.ToString(), href);                  }              }              return content;      }  }

你是否考慮過這樣的寫法

using System;  using System.Collections.Generic;  using System.Text;   using System.Text.RegularExpressions;  using System.Web;   public class UrlUBB  {          private static Regex reg = new Regex(@"\(url=(?< url>.[^\)]*)\)(?< name>.[^\(]*)\(/url\)");           /// < summary>          /// 替換UBB里面的url          /// < /summary>          /// < param name="content">< /param>          /// < returns>< /returns>          public static string RegularUrl(string content)          {              if (string.IsNullOrEmpty(content))                  return string.Empty;               if (content.IndexOf(@"(url=", StringComparison.OrdinalIgnoreCase) == -1 || content.IndexOf(@"(/url)", StringComparison.OrdinalIgnoreCase) == -1)                  return content;                string url = string.Empty;              string name = string.Empty;              string href = string.Empty;               MatchCollection matches = reg.Matches(content);              foreach (Match m in matches)              {                  if (m.Success)                  {                      url = regexUrl(m.Groups["url"].ToString());                      name = m.Groups["name"].ToString();                      href = string.Format("< a href=\"redirect.aspx?goto={0}\">{1}< /a>", url, name);                      content = content.Replace(m.ToString(), href);                  }              }              return content;      }  }

如果你的代碼不幸也占了那么一、兩個(gè),那么修改并對比一下性能試試看,如果你很幸運(yùn)的一個(gè)也沒占,那么恭喜你,你的程序性能應(yīng)該還不錯(cuò)。

關(guān)于“編程語言中可能引發(fā)性能問題的代碼寫法有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請把它分享出去讓更多的人看到。

網(wǎng)頁題目:編程語言中可能引發(fā)性能問題的代碼寫法有哪些
轉(zhuǎn)載注明:http://chinadenli.net/article46/gigdeg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、響應(yīng)式網(wǎng)站、搜索引擎優(yōu)化外貿(mào)建站、ChatGPT、網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司