-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStopWatch.cs
More file actions
124 lines (112 loc) · 2.89 KB
/
Copy pathStopWatch.cs
File metadata and controls
124 lines (112 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
using System;
using System.Runtime.InteropServices;
namespace CoreObjects
{
public class StopWatch
{
//Code-timing class by Alastair Dallas 11/25/02
//Based on Microsoft KB Article 172338 and 306978
//
//Use Kernel32 functions for near-microsecond timing of processes
//
//Usage:
//
// Dim swatch as New Stopwatch("My process")
// ...perform My process...
// swatch.ReportToConsole()
//
// Output: "My process (0.332131039000767 seconds)"
//
// Alternative:
//
// Dim firstResult, secondResult as Double
// Dim swatch as New Stopwatch()
// ...perform process...
// swatch.Done()
// firstResult = swatch.ElapsedTime()
// swatch.Start()
// ...more process work...
// swatch.Done()
// secondResult = swatch.ElapsedTime()
// Console.WriteLine("First took {0} seconds and Second took {1} seconds.",
// firstResult, secondResult)
//
// Output: "First took 0.0131530683368976 seconds and Second took 8.8372527793337 seconds."
[DllImport("Kernel32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
public static extern short QueryPerformanceCounter(ref long X);
[DllImport("Kernel32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
public static extern short QueryPerformanceFrequency(ref long X);
private long _start;
private long _end;
private long _freq;
private long _overhead;
private string _desc;
public StopWatch()
: this(string.Empty)
{
}
public StopWatch(string description)
{
if (QueryPerformanceCounter(ref _start) == 0)
{
throw (new StopwatchException());
}
if (QueryPerformanceCounter(ref _end) == 0)
{
throw (new StopwatchException());
}
// calculate the time just to call start and end
_overhead = _end - _start;
this.Start(description);
}
public void Start(string description)
{
_desc = description;
QueryPerformanceFrequency(ref _freq);
_end = 0;
if (QueryPerformanceCounter(ref _start) == 0)
{
throw (new StopwatchException());
}
}
public double Done()
{
if (QueryPerformanceCounter(ref _end) == 0)
{
throw (new StopwatchException());
}
return this.ElapsedTime();
}
public double ElapsedTime()
{
if (_end == 0)
{
long RightNow = 0;
if (QueryPerformanceCounter(ref RightNow) == 0)
{
throw (new StopwatchException());
}
return (((RightNow - _start) - _overhead) / _freq);
}
return (((_end - _start) - _overhead) / _freq);
}
public void ReportToConsole()
{
if (_end == 0)
{
this.Done();
}
Console.WriteLine(_desc + " ({0} seconds)", this.ElapsedTime());
}
}
public class StopwatchException : ApplicationException
{
override public string Message
{
get
{
return "Stopwatch: QueryPerformanceCounter[Kernel32] returned 0";
}
}
}
}