Function frequency: Difference between revisions

→‎{{header|REXX}}: added/changed whitespace and comments, add aligned numbers via computation of width, made the frequency counter more idiomatic.
(→‎{{header|REXX}}: added/changed whitespace and comments, add aligned numbers via computation of width, made the frequency counter more idiomatic.)
Line 932:
 
=={{header|REXX}}==
This version doesn't report on the top ten functions (or subroutines), only the functions that are been counted (as implemented below).
 
<br><br>There is no direct method of this type of counting behavior, but this technique allows a programmer to determine how many invocations there are for various functions/subroutines.
<br><br>The use of the '''?.''' stemmed variable is not specific and it can be any (seldom-used, or better yet, unused) REXX variable.
 
<br><br>The use of the &nbsp; <big>'''?.'''</big> &nbsp; stemmed variable is not specific and it can be any (seldom-used, or better yet, unused) REXX variable.
<br>[A question mark was chosen because it most likely won't be used in most REXX programs.]
<br>Also, the counter itself (the array index) should be unique (to avoid REXX variable name collisions).
<lang rexx>/*REXX pgmprogram counts frequency of various subroutine/function invocations. */
?.=0parse arg many . /*obtain optional argument from the /*initialize all funky countersCL. */
if many=='' | many==',' then many=20 /*Not specified? Then use the default.*/
do j=1 to 10
numeric digits 600 /*insure we can handle some big numbers*/
?.=0 /*initialize (et al) the freq counters.*/
do j=1 for many /* [↓] perform this loop MANY times.*/
factorial = !(j)
factorial_R = !rR(j)
fibonacci = fib(j)
fibonacci_R = fibR(j)
hofstadterQHofstadterQ = hofsQ(j)
width breadth = length(j) + length(length(j**j))
end /*j*/
/*max # of invokes [↓] */
 
say 'number of invocations for ! (factorial) = ' right(?.! , ?.w)
say 'number of invocations for ! recursive = =' right(?.!rR , ?.w)
say 'number of invocations for Fibonacci = ' right(?.fib , ?.w)
say 'number of invocations for Fib recursive = ' right(?.fibR , ?.w)
say 'number of invocations for Hofstadter Q = ' right(?.hofsQ , ?.w)
say 'number of invocations for LENGTHlength = ' right(?.length , ?.w)
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
 
?: arg _; ?._=?._+1; ?.w=max(?.w, 'LENGTH'(?._)); return
/*─────────────────────────────────────! (factorial) subroutine─────────*/
/*────────────────────────────────────────────────────────────────────────────*/
!: procedure expose ?.; ?.!=?.!+1; parse arg x; !=1
!: procedure expose ?.; call ? !; !=1; do j=2 to xarg(1); !=!*j; end; return !
/*────────────────────────────────────────────────────────────────────────────*/
 
!rR: procedure expose ?.; call ?. !r=?.!r+1R; parse arg x; if x<2 then return 1;return x*!R(x-1)
/*─────────────────────────────────────!r (factorial) subroutine────────*/
/*────────────────────────────────────────────────────────────────────────────*/
!r: procedure expose ?.; ?.!r=?.!r+1; parse arg x; if x<2 then return 1
!fib: procedure expose ?.; call ?.!=?.!+1 fib; parse arg xn; na=abs(n); a=0; !b=1
return x * !R(x-1)
if na<2 then return na /*test for couple special cases.*/
 
do j=2 to na; s=a+b; a=b; b=s; end
/*──────────────────────────────────FIB subroutine (non─recursive)──────*/
if n>0 | na//2==1 then return s /*if positive or odd negative... ···*/
fib: procedure expose ?.; ?.fib=?.fib+1; parse arg n; na=abs(n); a=0; b=1
if na<2 then return na else return -s /*testreturn fora couplenegative specialFib casesnumber. */
/*────────────────────────────────────────────────────────────────────────────*/
do j=2 to na; s=a+b; a=b; b=s; end
fibfibR: procedure expose ?.; call ?.fib=?.fib+1 fibR; parse arg n; na=abs(n); a=0; b s= 1
if n>0 | na//2==1 then return s /*if positive or odd negative... */
if na<2 then return na; else return -sif /*returnn<0 a negativethen Fib number.if *n//2==0 then s=-1
return (fibR(na-1) + fibR(na-2)) * s
 
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────FIBR subroutine (recursive)─────────*/
fibRhofsQ: procedure expose ?.; call ?.fibR=?.fibr+1 hofsq; parse arg n; na=abs( if n);<2 then return s=1
if na<2 then return na /*handle a couple special cases. */ return hofsQ(n-hofsQ(n-1)) + hofsQ(n-hofsQ(n-2))
/*────────────────────────────────────────────────────────────────────────────*/
if n <0 then if n//2==0 then s=-1
length: procedure expose ?.; call ?. length=?.length+; return 'LENGTH'(arg(1))</lang>
return (fibR(na-1)+fibR(na-2))*s
'''output''' &nbsp; when using the default input of: <tt> xxx </tt>
 
<pre>
/*──────────────────────────────────HOFSQ subroutine (recursive)────────*/
number of invocations for ! (factorial) = 10 20
hofsQ: procedure expose ?.; ?.hofsq=?.hofsq+1; parse arg n
number of invocations for ! recursive = 55 210
if n<2 then return 1
number of invocations for Fibonacci = 10 20
return hofsQ(n - hofsQ(n - 1)) + hofsQ(n - hofsQ(n - 2))
number of invocations for Fib recursive = 45257290
 
number of invocations for Hofstadter Q = 1922308696
/*──────────────────────────────────LENGTH subroutine───────────────────*/
number of invocations for LENGTHlength = 30 60
length: procedure expose ?.; ?.length=?.length+1
return 'LENGTH'(arg(1))</lang>
'''output''' when using the input of: <tt> xxx </tt>
<pre style="overflow:scroll">
number of invocations for ! (factorial) = 10
number of invocations for ! recursive = 55
number of invocations for Fibonacci = 10
number of invocations for Fib recursive = 452
number of invocations for Hofstadter Q = 1922
number of invocations for LENGTH = 30
</pre>