





CodePudding user response:
Use a Stopwatch to time:var clock=System. Diagnostics. Stopwatch. StartNew ();
//time-consuming operation,,,
Var takes milliseconds=clock. ElapsedMilliseconds;
CodePudding user response:
If it is for each of the sorting method consumes time, it must not use the timer control, timer control is at regular intervals to perform a task, should use the. NET built-in Stopwatch class,using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. The Text;
Using System. The Threading. The Tasks;
The namespace ConsoleApplication1
{
Class Program
{
The static void Main (string [] args)
{
Int [] nums={3, 2, 1};
//start the time
System. Diagnostics. Stopwatch watch=new System. Diagnostics. The Stopwatch ();
Watch the Reset ();
Watch. Start ();
//stop timing
SortMethodOne (nums);
Watch. Stop ();
Console. WriteLine (" methods used by one time is: {0} milliseconds ", watch. ElapsedMilliseconds);
//to timing
Watch. Restart ();
SortMethodTwo (nums);
Watch. Stop ();
Console. WriteLine (" time is used for the method 2: "{0} milliseconds, watch. ElapsedMilliseconds);
}
The static void SortMethodOne (int [] nums)
{
//a sorting method takes 3000 milliseconds
System. Threading. Thread. Sleep (3000);
}
The static void SortMethodTwo (int [] nums)
{
//sorting method 2 5000 milliseconds
System. Threading. Thread. Sleep (5000);
}
}
}
CodePudding user response:
First of all, record the current timeAnd operating your order
Then write down the time
Then the time - the consumption of the current time is your time
CodePudding user response:
Thank you very much,
CodePudding user response: