In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail the code writing methods that may cause performance problems in the programming language. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
1. Int.Parse () VS int.TryParse ()
Is that what you're writing?
Int id = 0; try {id = int.Parse (Request ["id"]);} catch {id = 0;}
If it is, you can try it this way.
Int id = 0; int.TryParse (Request ["id"], out id)
The former way of writing, once Request ["id"] is non-numeric, will throw an exception, the cost of throwing an exception is very large, while the latter will not throw any exception.
2.string.IndexOf ()
Is that what you're writing?
String s = "aaa,bb"; int pos = s.IndexOf (",")
In fact, for the search of a single character, it is better to write this way.
String s = "aaa,bb"; int pos = s.IndexOf (',')
Some people may ask, if I am looking for more than one character, I can try the following
String s = "aaa,bb"; int pos = s.IndexOf ("bb", StringComparison.OrdinalIgnoreCase)
The specific usage of StringComparison can be obtained by google or baidu.
3. RegexOptions.Compiled
If you are using regularization and using this parameter, please be careful, according to your personal experience, using this parameter may cause performance problems in the case of high traffic, such as high cpu. If you are in doubt, you can try to compare which performance is better with and without this parameter.
4. Forgot to close the database connection
Database connections are very resourceful, so it should be as short as possible from opening to closing. To see if you forgot to turn it off, you can check it through the performance Monitor. Net Data provider for SqlClient (assuming you are using sqlserver). The specific parameter description can be obtained through google and baidu.
5. Frequent Response.Write ()
Is that what you're doing?
Response.Write ("this is line 1.
< br/>"); Response.Write (" this is line 2.
< br/>"); Response.Write (" this is line 3.
< br/>")
This way of writing frequently calls Response.Write (), which, according to experience, consumes a lot of cpu. Try the following
StringBuilder sb = new StringBuilder (); sb.Append ("this is line 1.
< br/>"); sb.Append (" this is line 2.
< br/>"); sb.Append (" this is line 3.
< br/>"); Response.Write (sb.ToString ())
6. Unnecessary repetition of operations
List
< TopicInfo>List = new List
< TopicInfo>(); / / get the data from the database list = GetDataFromDB (); for (int I = 0 position I)
< list.Count; i++ ) { TopicInfo item = list[i]; } 上面的代码估计谁都看的出来有什么不好,那下面这个代码呢 using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using System.Web; public class UrlUBB { /// < summary>/ replace url / in UBB
< /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;}
Have you ever considered this way of writing?
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>/ replace url / in UBB
< /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;}
If your code unfortunately accounts for one or two, then modify and compare the performance to try, if you are lucky to have none, then congratulations, your program performance should be good.
This is the end of this article on "what are the code writing methods that may cause performance problems in programming languages?". I hope the above content can be helpful to you, so that you can learn more knowledge. If you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.