Net: how to calculate the efficiency of a piece of code, net computing efficiency

Source: Internet
Author: User

Net: how to calculate the efficiency of a piece of code, net computing efficiency

In.. Net 4.0 and later versions provide a class in the System. in the Diagnostics namespace, this class can be used to calculate the efficiency of codes at both ends with the same execution results, which is very practical in code optimization.

Is the generic efficiency high or low ?? Let's test the Code as follows:

 

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace ConsoleApp1 {class Program {static void Main (string [] args) {Console. writeLine ("\ r \ n"); System. diagnostics. stopwatch watch = new System. diagnostics. stopwatch (); watch. start (); for (int I = 0; I <10000000; I ++) {ShowObj (123);} Console. writeLine ("Number of milliseconds for object method execution" + watch. elapsedMilliseconds); watch. stop () ;}{ System. diagnostics. stopwatch watch = new System. diagnostics. stopwatch (); watch. start (); for (int I = 0; I <10000000; I ++) {ShowGeneric (123);} Console. writeLine ("Number of milliseconds executed by the generic method" + watch. elapsedMilliseconds); watch. stop ();} Console. read ();} public static void ShowObj (object obj) {string str = "123";} public static void ShowGeneric <T> (T t) {string str = "123 ";}}}

  

 

 

 

After the above execution, we can change the help classes in our program framework to generic ones.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.