Difference between revisions of "Os.clock"

From ComputerCraft Wiki
Jump to: navigation, search
(use of os.clock())
 
Line 1: Line 1:
Returns the amount of time the computer craft computer has been running, note that this may not be the amount of time the program has been running. the function os.clock() is useful for debugging, for example to see how long a custom function takes to execute use the following code:
+
{{lowercase}}
 
+
{{Function
<code>x = os.clock() -- compensates for time already passed
+
|name=os.clock
myfunction() -- function to be tested
+
|returns=[[float (type)|float]], amount of time the computercraft computer has been running.
y = (os.clock() - x) -- calculates time change
+
|api=os
print(y)
+
|addon=ComputerCraft
 
+
|desc=Returns the amount of time the computer craft computer has been running, note that this may not be the amount of time the program has been running. the function os.clock() is useful for debugging, see the example.
 
+
|examples=
</code>
+
{{Example
 +
|desc=Measure how long a custom function takes to execute
 +
|code=x = os.clock() -- compensates for time already passed<br />myfunction() -- function to be tested<br />y = (os.clock() - x) -- calculates time change<br />print(y)
 +
|output=Time it took to run myFunction()
 +
}}
 +
}}

Revision as of 07:50, 30 May 2012


Grid Redstone.png  Function os.clock
Returns the amount of time the computer craft computer has been running, note that this may not be the amount of time the program has been running. the function os.clock() is useful for debugging, see the example.
Syntax os.clock()
Returns float, amount of time the computercraft computer has been running.
Part of ComputerCraft
API os

Examples

Grid paper.png  Example
Measure how long a custom function takes to execute
Code
x = os.clock() -- compensates for time already passed
myfunction() -- function to be tested
y = (os.clock() - x) -- calculates time change
print(y)
Output Time it took to run myFunction()