You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.2 KiB
48 lines
1.2 KiB
using System; |
|
using System.Runtime.InteropServices; |
|
|
|
namespace hires |
|
{ |
|
public class Stopwatch |
|
{ |
|
[DllImport("Kernel32.dll")] |
|
private static extern bool QueryPerformanceCounter( |
|
out long lpPerformanceCount); |
|
|
|
[DllImport("Kernel32.dll")] |
|
private static extern bool QueryPerformanceFrequency( |
|
out long lpFrequency); |
|
|
|
private long start=0; |
|
private long stop=0; |
|
|
|
// static - so this value used in all instances of |
|
private static double frequency = getFrequency(); |
|
|
|
// Note this is static- called once, before any constructor! |
|
private static double getFrequency() |
|
{ |
|
long tempfrequency; |
|
QueryPerformanceFrequency(out tempfrequency); |
|
return tempfrequency; // implicit casting to double from long |
|
} |
|
|
|
public void Start() |
|
{ |
|
QueryPerformanceCounter(out start); |
|
} |
|
|
|
public void Stop() |
|
{ |
|
QueryPerformanceCounter(out stop); |
|
} |
|
|
|
public double Elapsed |
|
{ |
|
get |
|
{ |
|
return (double)(stop - start) / frequency; |
|
} |
|
} |
|
} |
|
}
|
|
|