Method Function.time
Get the execution time of a function
-
Syntax
Object Function.time(Number n , Mixed obj , [ Mixed a2 ... an ])
This method will time a functionntimes.
Ifobjis null then the function is timed as a standard function. Ifobjis a valid object reference then the function is timed as a method ofobj.
Any extra argumentsa2...anare passed to the function/method being timed.
A defaulttoStringmethod is assigned to the return object allowing for easy interpretation of the result. -
Parameters
Number n- The number of times to run the test.
Mixed obj- Pass
nullto time the function as a normal function. If you pass an object reference the function is executed as a method ofobj. Mixed a2 ... an(Optional)- The arguments you wish to pass to the function/method being tested
-
Return value
A custom object
objwith following properties
obj.f - A reference to the function being timed
obj.n - The number of times the function was executed
obj.mean - The average time for a function execution
obj.s - The variation
obj.sd - The standard deviation
All times reported are in milliseconds. -
Examples
- To test the execution time of the function f 25 times
f.time(25);
- To test the execution time of the function fnc(a,b) {a * b;} 25 times passing the arguments 10 and 34 to fnc
fnc.time(25,null,10,34);
- To test the execution time of the function fnc(a,b) {a * b;} 25 times as a method of object o, passing the arguments 10 and 34 to fnc
fnc.time(25,o,10,34);
-
Download
Source
Unless otherwise noted all code in the JSLab Standard Library is licensed as GPLv3. See http://www.gnu.org/licenses/gpl.html for the entire license.
This function has dependencies and will not work stand-alone. Use the download button above instead of copy/paste.
