C # Records cycle consumption time

Source: Internet
Author: User

Today wrote the Loop section code, but feel like the performance is very poor appearance, want to look at the entire cycle of execution time, at first I think of DateTime.Now, but the strange thing is, if I cycle less than 30,000 times (less than a few words) will find 2 time interval is the same! Whether it is DateTime.Now.Tick or DateTime.Now.TimeOfDay.ToString (), the Internet to find information, it seems that datetime.now accuracy is relatively low, looking for a long time finally find a solution, direct sticker Code

Private Static voidTest () { Do{Console.Write ("Number:"); intnum =Convert.ToInt32 (Console.ReadLine ()); Stopwatch SW=NewStopwatch (); Sw.                Start ();  for(inti =0; i < num; i++)                {                    intA =1; } SW.                 Stop (); Console.WriteLine ("spents:"+SW.                Elapsedticks); Console.WriteLine ("*******************************"); }  while(1==1); }

C # Records cycle consumption time

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.