The running speed of the software must be within a range acceptable to the user. Often, improving the speed of those short-lived but frequently used routines will greatly increase the overall speed of the software.
To improve speed, of course, you first need to be able to measure time. Okay, then let's consider the situation on the track. As soon as the gun sounds, press the stopwatch to start timing, and end the timing when the player reaches the finish line. At this time, you can know the time used by the player. Before starting the next round, the stopwatch must be reset to zero. .NET2.0 also provides such a stopwatch: Stopwatch class, which can measure time more accurately.
Speed test:
Software performance and testability is a complex topic. Ensuring that an application meets user expectations requires considering its performance and testability during the development cycle. This is crucial during the design phase, a poor design will almost certainly lead to a poor user experience. However, good design alone does not guarantee that the program will run efficiently. The quality of the final code is equally important.
Measuring a long-running routine is fairly simple. If a process lasts several minutes, a watch can be used to record the time. For example, for a process that takes two minutes to execute, a 10% improvement can save 12 seconds, which is easy to determine.
And if you want to measure a very short-term process, you have to consider better accuracy. For example, there are some very small routines that may only take a thousandth of a second to run, but will be called 1 million times. The cumulative effect is obvious. In previous versions of the .NET framework, you needed to use Windows API functions, and in .NET framework 2.0, Microsoft introduced the Stopwatch (it is our stopwatch) class to simplify time measurement tasks.
Stopwatch class:
Using the Stopwatch class to measure time is very simple. Like stopwatches in real life, objects of this class can also start, stop, and return to zero (reset) the counter, but it is much more accurate than ordinary stopwatches. It can be accurate to microseconds (that is, millions of seconds). fraction of a second).
Sample code:
To demonstrate the use of Stopwatch, let's use a piece of code. Here is a console application that accumulates all integers between 1 and 1 million:
using System;
namespace StopWatchClass
{
class Program
{
static void Main(string[] args)
{
long total = 0;
for (int i = 1; i <= 10000000; i++)
{
total += i;
}
}
}
}
Add Stopwatch object:
The Stopwatch class is located in the System.Diagnostics namespace. Here is the code after adding the object:
using System;
using System.Diagnostics;
namespace StopWatchClass
{
class Program
{
static void Main(string[] args)
{
Stopwatch timer = new Stopwatch();
long total = 0;
for (int i = 1; i <= 10000000; i++)
{
total += i;
}
}
}
}
Control the Stopwatch object:
Stopwatch provides several methods to control the Stopwatch object. The Start method starts a timing operation, and the Stop method stops timing. At this time, if the Start method is used for the second time, timing will continue, and the final timing result is the accumulation of the two timings. To avoid this situation, use the Reset method to reset the object to zero before the second timer. None of these three methods require parameters. The code is:
using System;
using System.Diagnostics;
namespace StopWatchClass
{
class Program
{
static void Main(string[] args)
{
Stopwatch timer = new Stopwatch();
long total = 0;
timer.Start();
for (int i = 1; i <= 10000000; i++)
{
total += i;
}
timer.Stop();
}
}
}
Read Stopwatch results:
<!--[if !supportLists]--><!--[endif]--> After ending the timing, the next step is to read the timing results. The Stopwatch class provides the following properties:
<!--[if !supportLists]--><!--[endif]--><!--[if !supportLists]--><!--[endif]-->
Elapsed: Returns a TimeSpan object, representing the timing interval;
ElapsedMilliseconds: Returns the number of microseconds passed by the timing. The accuracy is slightly less, suitable for slightly longer timing;
ElapsedTicks: Returns the number of timer ticks elapsed. A timer tick is the smallest possible unit of measurement for a Stopwatch object. The length of the timer tick is determined by the specific computer and operating system. The value of the Frequency static field of the Stopwatch object represents the number of timer ticks contained in one second. Note the difference in the time units used by the TimeSpan's Ticks property.
One of these properties should be selected based on the timing task. In our example program, the Elapsed property provides the required precision to output the number of elapsed microseconds. This is also the highest accuracy of TimeSpan.
Here is the final program code:
using System;
using System.Diagnostics;
namespace StopWatchClass
{
class Program
{
static void Main(string[] args)
{
Stopwatch timer = new Stopwatch();
long total = 0;
timer.Start();
for (int i = 1; i <= 10000000; i++)
{
total += i;
}
timer.Stop();
decimal micro = timer.Elapsed.Ticks / 10m;
Console.WriteLine("Execution time was {0:F1} microseconds.", micro);
}
}
}
In addition, use the IsRunning property to check whether a Stopwatch instance is timing, and use the StartNew method to start a new timer.