-
Notifications
You must be signed in to change notification settings - Fork 0
/
StopWatch.lua
55 lines (41 loc) · 1.19 KB
/
StopWatch.lua
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
local ffi = require"ffi"
require "win_kernel32"
local class = require "class"
class.StopWatch()
local kernel32 = ffi.load("kernel32")
function StopWatch:_init()
self.Frequency = 0
self.StartCount = 0
self:Reset();
end
function StopWatch:__tostring()
return string.format("Frequency: %d Count: %d", self.Frequency, self.StartCount)
end
function StopWatch:GetCurrentTicks()
return GetPerformanceCounter()
end
--[[
/// <summary>
/// Reset the startCount, which is the current tick count.
/// This will reset the elapsed time because elapsed time is the
/// difference between the current tick count, and the one that
/// was set here in the Reset() call.
/// </summary>
--]]
function StopWatch:Reset()
self.Frequency = 1/GetPerformanceFrequency();
self.StartCount = GetPerformanceCounter();
end
-- <summary>
-- Return the number of seconds that elapsed since Reset() was called.
-- </summary>
-- <returns>The number of elapsed seconds.</returns>
function StopWatch:Seconds()
local currentCount = GetPerformanceCounter();
local ellapsed = currentCount - self.StartCount
local seconds = ellapsed * self.Frequency;
return seconds
end
function StopWatch:Milliseconds()
return self:Seconds() * 1000
end