Original link:
http://www.cnblogs.com/wysky/archive/2007/12/06/985779.html
Simply using beginDate - endDate inaccurate, is always a multiple of a certain value, such as a multiple of 0.015625 seconds, the value is small, simply that a 0 (0-fold).
.NET Framework v2.0 There is a new System.Diagnostics.Stopwatch object paper that is used to calculate the time difference with it, .NET 2.0 is ready, so the 2.0, this article does not make sense, as long as this know objects on it usage is quite simple, for example:
Not available in 1.1 years, but only to complete the target of anti-compiled code Copy out a self Stopwatch objects, do a little change on OK. Example code:
2 at compile time have found an error, unable to find SafeNativeMethods object because it is internal access restrictions. In this class uses two methods SafeNativeMethods in, QueryPerformanceFrequency and QueryPerformanceCounter, also decompile them out directly into the Stopwatch class among self, or self-built a SafeNativeMethods same class in the same namespace on Stopwatch it.
.NET Framework v2.0 There is a new System.Diagnostics.Stopwatch object paper that is used to calculate the time difference with it, .NET 2.0 is ready, so the 2.0, this article does not make sense, as long as this know objects on it usage is quite simple, for example:
public
void SomeMethod () { The Stopwatch Stopwatch = Stopwatch.StartNew (); // do something // just a simple example, Stopwatch also provides other properties and methods in more detail. Console.WriteLine ( " the Elapsed Seconds The: {0} " , stopwatch.Elapsed.TotalSeconds); }
Not available in 1.1 years, but only to complete the target of anti-compiled code Copy out a self Stopwatch objects, do a little change on OK. Example code:
using
System;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
namespace Zealot.Framework.Web.Utils
{
/// <summary>
/// v1.1 没有 Stopwatch,从 v2.0 复制出来
/// </summary>
public class Stopwatch
{
// Fields
private long elapsed;
public static readonly long Frequency;
public static readonly bool IsHighResolution;
private bool isRunning;
private long startTimeStamp;
private static readonly double tickFrequency;
private const long TicksPerMillisecond = 0x2710 ;
private const long TicksPerSecond = 0x989680 ;
// Methods
static Stopwatch()
{
if ( ! SafeNativeMethods.QueryPerformanceFrequency( out Frequency))
{
IsHighResolution = false ;
Frequency = 0x989680 ;
tickFrequency = 1 ;
}
else
{
IsHighResolution = true ;
tickFrequency = 10000000 ;
tickFrequency /= ( double )Frequency;
}
}
public Stopwatch()
{
this .Reset();
}
private long GetElapsedDateTimeTicks()
{
long rawElapsedTicks = this .GetRawElapsedTicks();
if (IsHighResolution)
{
double num2 = rawElapsedTicks;
num2 *= tickFrequency;
return ( long )num2;
}
return rawElapsedTicks;
}
private long GetRawElapsedTicks()
{
long elapsed = this .elapsed;
if ( this .isRunning)
{
long num3 = GetTimestamp() - this .startTimeStamp;
elapsed += num3;
}
return elapsed;
}
public static long GetTimestamp()
{
if (IsHighResolution)
{
long num = 0 ;
SafeNativeMethods.QueryPerformanceCounter( out num);
return num;
}
return DateTime.UtcNow.Ticks;
}
public void Reset()
{
this .elapsed = 0 ;
this .isRunning = false ;
this .startTimeStamp = 0 ;
}
public void Start()
{
if ( ! this .isRunning)
{
this .startTimeStamp = GetTimestamp();
this .isRunning = true ;
}
}
public static Stopwatch StartNew()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
return stopwatch;
}
public void Stop()
{
if ( this .isRunning)
{
long num2 = GetTimestamp() - this .startTimeStamp;
this .elapsed += num2;
this .isRunning = false ;
}
}
// Properties
public TimeSpan Elapsed
{
get
{
return new TimeSpan( this .GetElapsedDateTimeTicks());
}
}
public long ElapsedMilliseconds
{
get
{
return ( this .GetElapsedDateTimeTicks() / (( long ) 0x2710 ));
}
}
public long ElapsedTicks
{
get
{
return this .GetRawElapsedTicks();
}
}
public bool IsRunning
{
get
{
return this .isRunning;
}
}
}
}
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
namespace Zealot.Framework.Web.Utils
{
/// <summary>
/// v1.1 没有 Stopwatch,从 v2.0 复制出来
/// </summary>
public class Stopwatch
{
// Fields
private long elapsed;
public static readonly long Frequency;
public static readonly bool IsHighResolution;
private bool isRunning;
private long startTimeStamp;
private static readonly double tickFrequency;
private const long TicksPerMillisecond = 0x2710 ;
private const long TicksPerSecond = 0x989680 ;
// Methods
static Stopwatch()
{
if ( ! SafeNativeMethods.QueryPerformanceFrequency( out Frequency))
{
IsHighResolution = false ;
Frequency = 0x989680 ;
tickFrequency = 1 ;
}
else
{
IsHighResolution = true ;
tickFrequency = 10000000 ;
tickFrequency /= ( double )Frequency;
}
}
public Stopwatch()
{
this .Reset();
}
private long GetElapsedDateTimeTicks()
{
long rawElapsedTicks = this .GetRawElapsedTicks();
if (IsHighResolution)
{
double num2 = rawElapsedTicks;
num2 *= tickFrequency;
return ( long )num2;
}
return rawElapsedTicks;
}
private long GetRawElapsedTicks()
{
long elapsed = this .elapsed;
if ( this .isRunning)
{
long num3 = GetTimestamp() - this .startTimeStamp;
elapsed += num3;
}
return elapsed;
}
public static long GetTimestamp()
{
if (IsHighResolution)
{
long num = 0 ;
SafeNativeMethods.QueryPerformanceCounter( out num);
return num;
}
return DateTime.UtcNow.Ticks;
}
public void Reset()
{
this .elapsed = 0 ;
this .isRunning = false ;
this .startTimeStamp = 0 ;
}
public void Start()
{
if ( ! this .isRunning)
{
this .startTimeStamp = GetTimestamp();
this .isRunning = true ;
}
}
public static Stopwatch StartNew()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
return stopwatch;
}
public void Stop()
{
if ( this .isRunning)
{
long num2 = GetTimestamp() - this .startTimeStamp;
this .elapsed += num2;
this .isRunning = false ;
}
}
// Properties
public TimeSpan Elapsed
{
get
{
return new TimeSpan( this .GetElapsedDateTimeTicks());
}
}
public long ElapsedMilliseconds
{
get
{
return ( this .GetElapsedDateTimeTicks() / (( long ) 0x2710 ));
}
}
public long ElapsedTicks
{
get
{
return this .GetRawElapsedTicks();
}
}
public bool IsRunning
{
get
{
return this .isRunning;
}
}
}
}
2 at compile time have found an error, unable to find SafeNativeMethods object because it is internal access restrictions. In this class uses two methods SafeNativeMethods in, QueryPerformanceFrequency and QueryPerformanceCounter, also decompile them out directly into the Stopwatch class among self, or self-built a SafeNativeMethods same class in the same namespace on Stopwatch it.
using
System;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
namespace Zealot.Framework.Web.Utils
{
[SuppressUnmanagedCodeSecurity]
internal class SafeNativeMethods
{
[DllImport( " kernel32.dll " )]
internal static extern bool QueryPerformanceCounter( out long value);
[DllImport( " kernel32.dll " )]
internal static extern bool QueryPerformanceFrequency( out long value);
}
}
Transfer from: http: //www.zealotforce.net/reply-638.aspx
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
namespace Zealot.Framework.Web.Utils
{
[SuppressUnmanagedCodeSecurity]
internal class SafeNativeMethods
{
[DllImport( " kernel32.dll " )]
internal static extern bool QueryPerformanceCounter( out long value);
[DllImport( " kernel32.dll " )]
internal static extern bool QueryPerformanceFrequency( out long value);
}
}
Reproduced in: https: //www.cnblogs.com/wysky/archive/2007/12/06/985779.html