Added some safety precautions and added documentation

This commit is contained in:
2019-02-08 13:06:42 -05:00
parent 9c5e055917
commit 88cf1b96d6

View File

@@ -1,51 +1,99 @@
--luaClasses/Stopwatch.lua
--Matthew Ellison
-- Created: 02-01-19
--Modified: 02-06-19
--Modified: 02-08-19
--This is a simple class to be used to time runtimes of various things within programs
Stopwatch = {
startTime = 0,
stopTime = 0
}
Stopwatch.__index = Stopwatch
--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 = {}
setmetatable(timer, Stopwatch)
return timer
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.startTime = os.clock()
self.stopTime = 0
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()
self.stopTime = os.clock()
--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 = 0
self.stopTime = 0
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()
return (self.stopTime - self.startTime)
local timeDifference = nil;
--If start and stop has been called return the difference
if(startTime and stopTime) then
timeDifference = endTime - 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(startTime and not stopTime) then
timeDifference = os.time() - 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()
return math.floor(self:getTime() * 1000000)
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()
return (self:getTime() * 1000)
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()
return self:getTime()
--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()
return (self:getTime() / 60)
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