mirror of
https://bitbucket.org/Mattrixwv/luaclasses.git
synced 2025-12-06 18:33:59 -05:00
100 lines
3.4 KiB
Lua
100 lines
3.4 KiB
Lua
--luaClasses/Stopwatch.lua
|
|
--Matthew Ellison
|
|
-- Created: 02-01-19
|
|
--Modified: 02-08-19
|
|
--This is a simple class to be used to time runtimes of various things within programs
|
|
|
|
|
|
--You need some kind of table to hold the times
|
|
Stopwatch = {}
|
|
Stopwatch.__index = Stopwatch;
|
|
|
|
--This is needed to create a Stopwatch and link the variables and functions
|
|
--It should be called before anything else when createing a Stopwatch
|
|
function Stopwatch:create()
|
|
local timer = {
|
|
startTime = nil,
|
|
stopTime = nil
|
|
};
|
|
setmetatable(timer, Stopwatch); --This links the new variable with the functions from the Stopwatch
|
|
return timer;
|
|
end
|
|
|
|
--This function gets the start time to simulate actually hitting the button on a stopwatch
|
|
function Stopwatch:start()
|
|
self.stopTime = nil; --Make sure it knows that the clock is still running
|
|
--Setting the clock last puts the time closer to the actual start of the thing you are trying to time
|
|
self.startTime = os.clock(); --Get the current time
|
|
end
|
|
|
|
--This function gets the stop time to simulate actually hitting the stop button on a stopwatch
|
|
function Stopwatch:stop()
|
|
--Set the time first, for efficiency sake
|
|
self.stopTime = os.clock();
|
|
--Make sure the clock had been started
|
|
if(startTime == nil) then
|
|
--If not unset the stop time
|
|
stopTime = nil;
|
|
end
|
|
end
|
|
|
|
--This function resets all the variables. It is used if you want to use the same timer for multiple tests, but it shouldn't be necessary. Use to avoid unforseen bugs
|
|
function Stopwatch:reset()
|
|
self.startTime = nil;
|
|
self.stopTime = nil;
|
|
end
|
|
|
|
--Returns the difference in the time at the default time resolution
|
|
--Mostly called from the other get___ functions
|
|
function Stopwatch:getTime()
|
|
local timeDifference = nil;
|
|
--If start and stop has been called return the difference
|
|
if((self.startTime ~= nil) and (self.stopTime ~= nil)) then
|
|
timeDifference = self.stopTime - self.startTime;
|
|
--If start has been called but stop hasn't make the current time the end time. This simulates looking at a stopwatch while it is still running
|
|
elseif((self.startTime ~= nil) and (self.stopTime == nil)) then
|
|
timeDifference = os.time() - self.startTime;
|
|
--Otherwise return -1 as an error
|
|
else
|
|
timeDifference = -1;
|
|
end
|
|
--Return the number generated
|
|
return timeDifference;
|
|
end
|
|
|
|
--This function returns the duration the stopwatch has run in microseconds
|
|
function Stopwatch:getMicroseconds()
|
|
local timeDifference = self:getTime(); --Get the time that has elapsed
|
|
--If you did not get an error convert the time to microseconds
|
|
if(timeDifference >= 0) then
|
|
timeDifference = timeDifference * 1000000;
|
|
end
|
|
return timeDifference;
|
|
end
|
|
|
|
--This function returns the duration the stopwatch has run in milliseconds
|
|
function Stopwatch:getMilliseconds()
|
|
local timeDifference = self:getTime(); --Get the time that has elapsed
|
|
--If you did not get an error convert the time to milliseconds
|
|
if(timeDifference >= 0) then
|
|
timeDifference = timeDifference * 1000;
|
|
end
|
|
return timeDifference;
|
|
end
|
|
|
|
--This function returns the duration the stopwatch has run in seconds
|
|
function Stopwatch:getSeconds()
|
|
--The function returns in seconds by default
|
|
return self:getTime();
|
|
end
|
|
|
|
--This function returns the duration the stopwatch has run in minutes
|
|
function Stopwatch:getMinutes()
|
|
local timeDifference = self:getTime();
|
|
--If you did not get an error conver the time to minutes
|
|
if(timeDifference >= 0) then
|
|
timeDifference = timeDifference / 60;
|
|
end
|
|
return timeDifference;
|
|
end
|