Home > Net >  How to use the timer component in c # to compare each time sorting method
How to use the timer component in c # to compare each time sorting method

Time:10-04

I click on the quick sort, sorting method is right but the elapsed time does not display, excuse me, what's the problem, please help

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 time
And 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, beginners, understand now, I try

CodePudding user response:

refer to the second floor HerryDong 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);
}
}
}
  •  Tags:  
  • C#
  • Related