可能引發(fā)性能問(wèn)題的幾個(gè)代碼寫法總結(jié)
可能引發(fā)性能問(wèn)題的幾個(gè)代碼寫法,看看你占哪一個(gè).
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í)對(duì)于單字符的查找,這樣寫會(huì)更好
- string s = "aaa,bb";
- int pos = s.IndexOf(',');
有人會(huì)問(wèn)如果我要找多個(gè)字符呢,那可以試試下面的
- string s = "aaa,bb";
- int pos = s.IndexOf("bb", StringComparison.OrdinalIgnoreCase);
至于 StringComparison 的具體用法可以google或者baidu得到。
3. RegexOptions.Compiled
如果你正在使用正則并且用到了這個(gè)參數(shù),那請(qǐng)你慎重,根據(jù)個(gè)人經(jīng)驗(yàn)使用這個(gè)參數(shù)在訪問(wèn)量比較大的情況下可能會(huì)引發(fā)性能問(wèn)題,比如cpu偏高。如果你表示懷疑,可以嘗試比較使用和不用這個(gè)參數(shù)的情況下哪個(gè)性能會(huì)更好。
4.忘記關(guān)閉數(shù)據(jù)庫(kù)連接
數(shù)據(jù)庫(kù)連接是非常好資源的,所以從打開到關(guān)閉應(yīng)該越短越好。想看看有沒(méi)有忘記關(guān)閉,可以通過(guò)性能監(jiān)視器的 .net Data provider for SqlClient ( 假設(shè)你用的是sqlserver ) 來(lái)查看,具體的參數(shù)說(shuō)明可以通過(guò)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ù)庫(kù)得到數(shù)據(jù)
- list = GetDataFromDB();
- for(int i = 0;i < list.Count; i++ )
- {
- TopicInfo item = list[i];
- }
上面的代碼估計(jì)誰(shuí)都看的出來(lái)有什么不好,那下面這個(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;
- }
- }
你是否考慮過(guò)這樣的寫法
- 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è),那么修改并對(duì)比一下性能試試看,如果你很幸運(yùn)的一個(gè)也沒(méi)占,那么恭喜你,你的程序性能應(yīng)該還不錯(cuò)。
【編輯推薦】