Function frequency: Difference between revisions
Content added Content deleted
m (→{{header|Sidef}}: better comment) |
(→{{header|REXX}}: elided REXX version 1, renumbered the subsequent versions.) |
||
Line 933: | Line 933: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===version 1=== |
===version 1=== |
||
This version doesn't report on the top ten functions (or subroutines), only the functions that are been counted (as implemented below). |
|||
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. |
|||
The use of the <big>'''?.'''</big> 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 program counts frequency of various subroutine/function invocations. */ |
|||
parse arg many . /*obtain optional argument from the CL.*/ |
|||
if many=='' | many==',' then many=20 /*Not specified? Then use the default.*/ |
|||
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 = !R(j) |
|||
fibonacci = fib(j) |
|||
fibonacci_R = fibR(j) |
|||
HofstadterQ = hofsQ(j) |
|||
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(?.!R , ?.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 length =' right(?.length , ?.w) |
|||
exit /*stick a fork in it, we're all done. */ |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
?: arg _; ?._=?._+1; ?.w=max(?.w, 'LENGTH'(?._)); return |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
!: procedure expose ?.; call ? !; !=1; do j=2 to arg(1); !=!*j; end; return ! |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
!R: procedure expose ?.; call ? !R; arg x;if x<2 then return 1;return x*!R(x-1) |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
fib: procedure expose ?.; call ? fib; parse arg n; na=abs(n); a=0; b=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 |
|||
if n>0 | na//2==1 then return s /*positive or odd negative ···*/ |
|||
else return -s /*return a negative Fib number. */ |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
fibR: procedure expose ?.; call ? fibR; parse arg n; na=abs(n); s= 1 |
|||
if na<2 then return na; if n<0 then if n//2==0 then s=-1 |
|||
return (fibR(na-1) + fibR(na-2)) * s |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
hofsQ: procedure expose ?.; call ? hofsq; parse arg n; if n<2 then return 1 |
|||
return hofsQ(n-hofsQ(n-1)) + hofsQ(n-hofsQ(n-2)) |
|||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
length: procedure expose ?.; call ? length; return 'LENGTH'(arg(1))</lang> |
|||
'''output''' when using the default input: |
|||
<pre> |
|||
number of invocations for ! (factorial) = 20 |
|||
number of invocations for ! recursive = 210 |
|||
number of invocations for Fibonacci = 20 |
|||
number of invocations for Fib recursive = 57290 |
|||
number of invocations for Hofstadter Q = 308696 |
|||
number of invocations for length = 60 |
|||
</pre> |
|||
===version 2=== |
|||
This program counts statically. It lacks, however, treatment of comments and literal strings. |
This program counts statically. It lacks, however, treatment of comments and literal strings. |
||
<lang rexx>fid='pgm.rex' |
<lang rexx>fid='pgm.rex' |
||
Line 1,045: | Line 985: | ||
1 translate</pre> |
1 translate</pre> |
||
===version |
===version 2=== |
||
This program counts statically. |
This program counts statically. |
||
Contents of comments and literal strings are not analyzed. |
Contents of comments and literal strings are not analyzed. |