Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize the performance of C #

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to optimize the performance of C#". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to optimize the performance of C#".

Reduce duplicate code

This is the most basic optimization solution, as far as possible to reduce the repetition of things, let them do only once, more common is this kind of code, the same Math.Cos (angle) and Math.Sin (angle) have been done twice.

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;}

After optimization

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;}

There is another way to instantiate an object in a method, but this object is actually reusable.

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

After optimization

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

There is also unnecessary initialization, such as no initialization before calling the out parameter.

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

New User () here is an unnecessary operation.

After optimization

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

Don't blindly believe in regular expressions

It just happens to be in * objects (Regex), by the way.

Many people think that regular expressions are very fast, very fast, super fast.

Although regular expressions are fast, don't be superstitious. Look at the chestnuts below.

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

The result is a 10-week cycle time, even with 10 Replace, is better than Regex, so don't be superstitious about him.

/ method 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); / / method 2 public static string ConvertQuot2 (string html) {return ReplaceQuot.Replace (html, ");}

ConvertQuot1:3518

ConvertQuot2:12479

I'll show you a real, miserable chestnut.

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, @")

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report