Time a function: Difference between revisions

→‎{{header|REXX}}: added a second version. -- ~~~~
m (→‎{{header|REXX}}: removed a leading blank from the ouput(s), added up/down arrows in comments, added DO-END comment label, changed indentation of DO loop. -- ~~~~)
(→‎{{header|REXX}}: added a second version. -- ~~~~)
Line 1,102:
 
=={{header|REXX}}==
===elapsed time version===
REXX doesn't have a language feature for obtaining true CPU time (except under IBM mainframes),
<br>butREXX it doesdoesn't have a built-inlanguage functionfeature for elapsedobtaining true CPU time (s).except under
<br>IBM mainframes which have commands that can retrieve such times), but it does
<br>have a built-in function for elapsed time(s).
<br>The main reason for the true CPU time omission is that REXX was developed under VM/CMS and
<br>there's a way to easily query the host (VM/CP) to indicate how much true CPU time was used by your userID.
<br>(normally) your own userID. The result can then be placed into a REXX variblevariable (as an option).
<lang rexx>/*REXX program to show the elapsed time for a function (or subroutine). */
arg times . /*get the arg from command line. */
Line 1,146 ⟶ 1,148:
function SILLY took 0.44 seconds.
</pre>
===CPU time used version===
This version only works with Regina REXX as the '''J''' option is a Regina extention.
<br><br>Since the '''SILLY''' function (by far) consumes the bulk of the CPU time of the REXX program,
<br>what is being measured is essentially the same as the the time of the function execution,
<br>the overhead of the invocation is minimal compared to the overall time used.
<lang rexx>/*REXX program shows the CPU time used for a REXX pgm since it started. */
arg times . /*get the arg from command line. */
if times=='' then times=100000 /*any specified? No, use default*/
junk = silly(times) /*invoke the SILLY function. */
/* CALL SILLY TIMES also works.*/
 
/*The J is for time used by */
/* │ the REXX program */
/* ┌────────┘ since it started, */
/* │ This is a Regina extension. */
/* ↓ */
say 'function SILLY took' format(time("J"),,2) 'seconds for' times "iteratations."
/* ↑ */
/* │ */
/* ┌────────────────┘ */
/* │ */
/* The above 2 for the FORMAT function displays the time */
/* with 2 decimal digits (past the decimal point). Using */
/* a 0 (zero) would round the time to whole seconds. */
exit
/*──────────────────────────────────SILLY subroutine────────────────────*/
silly: procedure /*chew up some CPU time doing some silly stuff.*/
do j=1 for arg(1) /*wash, apply, lather, rinse, repeat. ... */
a.j=random() date() time() digits() fuzz() form() xrange() queued()
end /*j*/
return j-1</lang>
'''output''' is essentially identical to the previous example.
<br><br>
 
=={{header|Ruby}}==