Difference between revisions of "love.timer.getTime"

(Added example.)
m
 
(12 intermediate revisions by 9 users not shown)
Line 1: Line 1:
Returns the amount of time since some time in the past.  
+
{{newin|[[0.3.2]]|032|type=function}}
This function should only be used to calculate differences between points in time, so exactly when this ''some time'' is should not matter.
+
Returns the value of a precise timer with an unspecified starting time.
 +
 
 +
This function should only be used to calculate differences between points in time, as the starting time of the timer is unknown prior to version [[11.4]]. Beginning with 11.4, the timer is initialized to 0 when the [[love.timer]] module is first loaded (usually before main.lua is loaded).
 
== Function ==
 
== Function ==
 
=== Synopsis ===
 
=== Synopsis ===
Line 9: Line 11:
 
None.
 
None.
 
=== Returns ===
 
=== Returns ===
{{param|number|time|The time in seconds.}}
+
{{param|number|time|The time in seconds. Given as a decimal, accurate to the microsecond.}}
 +
 
 
== Examples ==
 
== Examples ==
 
=== Checking how long something takes ===
 
=== Checking how long something takes ===
 
<source lang="lua">
 
<source lang="lua">
foo = ""
+
local start = love.timer.getTime()
stime = love.time.getTime()
 
  
for i=1,1000 do
+
-- Concatenate "bar" 1000 times.
 +
local foo = ""
 +
for _ = 1, 1000 do
 
foo = foo .. "bar"
 
foo = foo .. "bar"
 
end
 
end
  
etime = love.time.getTime()
+
-- Resulting time difference in seconds. Multiplying it by 1000 gives us the value in milliseconds.
print("It took " .. (etime-stime) .. " to concatenate 'bar' 1000 times!")
+
local result = love.timer.getTime() - start
 +
print( string.format( "It took %.3f milliseconds to concatenate 'bar' 1000 times!", result * 1000 ))
 
</source>
 
</source>
 
== See Also ==
 
== See Also ==
 
* [[parent::love.timer]]
 
* [[parent::love.timer]]
 
[[Category:Functions]]
 
[[Category:Functions]]
{{#set:Description=Returns the amount of time since some time in the past.  
+
{{#set:Description=Returns the precise amount of time since some time in the past.}}
}}
+
{{#set:Since=000}}
 +
{{#set:PrettySince=0.3.2}}
 +
== Other Languages ==
 +
{{i18n|love.timer.getTime}}

Latest revision as of 17:55, 30 December 2021

Available since LÖVE 0.3.2
This function is not supported in earlier versions.

Returns the value of a precise timer with an unspecified starting time.

This function should only be used to calculate differences between points in time, as the starting time of the timer is unknown prior to version 11.4. Beginning with 11.4, the timer is initialized to 0 when the love.timer module is first loaded (usually before main.lua is loaded).

Function

Synopsis

time = love.timer.getTime( )

Arguments

None.

Returns

number time
The time in seconds. Given as a decimal, accurate to the microsecond.

Examples

Checking how long something takes

local start = love.timer.getTime()

-- Concatenate "bar" 1000 times.
local foo = ""
for _ = 1, 1000 do
	foo = foo .. "bar"
end

-- Resulting time difference in seconds. Multiplying it by 1000 gives us the value in milliseconds.
local result = love.timer.getTime() - start
print( string.format( "It took %.3f milliseconds to concatenate 'bar' 1000 times!", result * 1000 ))

See Also


Other Languages