⍝ Performance profiling:

⍝ [profile] uses ⎕← to display profiling information in the session.
⍝ For testing, we modify the temporary copy of the operator in this
⍝ namespace to output profiling information directly as result. The
⍝ second (timing) column is removed from the result. Otherwise, small
⍝ variations in timing would cause the test to fail.

    s1 ← '⎕←trim⍕vcol,tcol' 'vcol'          ⍝ remove timing column
    s2 ← '1:shy←rslt⊣'      '⍪'             ⍝ return profiling information
    ⎕fx s1 subs s2 subs ⎕cr'profile'        ⍝ fix modified profile operator

    1 disp profile'disp' ⍳2 3 4             ⍝ hits for 1 disp ⍳2 3 4
┌──────────────────────────────────────────────────────────────────────────────────┐
│ 1 disp←{⎕IO ⎕ML←0 1                           ⍝ Boxed sketch of nested array.    │
│ 1                                                                                │
│ 1     ⍺←⍬ ⋄ dec ctd←2↑⍺                       ⍝ 1:decorated, 1:centred.          │
│ 1                                                                                │
│25     box←{                                   ⍝ Recursive boxing of nested array.│
│25         isor ⍵:⎕FMT⊂⍵                       ⍝ ⎕or: '∇name'.                    │
│25         1=≡,⍵:dec open ⎕FMT dec open ⍵      ⍝ simple array: format.            │
│ 1         mat←matr 1/dec open ⍵               ⍝ matrix of opened subarrays.      │
│ 1         r c←×⍴mat                           ⍝ non-null rows/cols.              │
│ 1         dec<0∊r c:c/r⌿∇ 1 open mat          ⍝ undecorated null: empty result.  │
│ 1         subs←aligned ∇¨mat                  ⍝ aligned boxed subarrays.         │
│ 1         (≢⍴⍵)gaps ⍵ plane subs              ⍝ collection into single plane.    │
│ 1     }                                                                          │
│ 1                                                                                │
│ 1     aligned←{                               ⍝ Alignment and centring.          │
│ 1         rows cols←sepr⍴¨⍵                   ⍝ subarray dimensions.             │
│ 1         sizes←(⌈/rows)∘.,⌈⌿cols             ⍝ aligned subarray sizes.          │
│ 1         ctd=0:sizes↑¨⍵                      ⍝ top-left alignment.              │
│           v h←sepr⌈0.5×↑(⍴¨⍵)-sizes           ⍝ vertical and horizontal rotation.│
│           v⊖¨h⌽¨sizes↑¨⍵                      ⍝ centred aligned subarrays.       │
│ 1     }                                                                          │
│ 1                                                                                │
│ 3     gaps←{                                  ⍝ Gap-separated sub-planes.        │
│ 3         ⍺≤2:⍵                               ⍝ lowish rank: done.               │
│ 1         subs←(⍺-1)∇¨⍵                       ⍝ sub-hyperplanes.                 │
│ 1         width←⊃⌽⍴⊃subs                      ⍝ width of inter-plane gap.        │
│ 1         fill←(⍺ width-3 0)⍴' '              ⍝ inter-plane gap.                 │
│ 1         ↑{⍺⍪fill⍪⍵}/1 open subs             ⍝ gap-separated planes.            │
│ 1     }                                                                          │
│ 1                                                                                │
│ 3     plane←{                                 ⍝ Boxed rank-2 plane.              │
│ 3         2<⍴⍴⍺:⍺ join ⍵                      ⍝ gap-separated sub-planes.        │
│ 2         odec←(dec shape ⍺)outer ⍵           ⍝ outer type and shape decoration. │
│ 2         idec←inner ⍺                        ⍝ inner type and shape decorations.│
│ 2         (odec,idec)collect ⍵                ⍝ collected, formatted subarrays.  │
│ 1     }                                                                          │
│ 1                                                                                │
│ 1     join←{                                  ⍝ Join of gap-separated sub-planes.│
│ 1         sep←(≢⍵)÷1⌈≢⍺                       ⍝ sub plane separation.            │
│ 1         split←(0=sep|⍳≢⍵)⊂[0]⍵              ⍝ separation along first axis.     │
│ 1         (⊂⍤¯1⊢⍺)plane¨split                 ⍝ sub-plane join.                  │
│ 1     }                                                                          │
│ 1                                                                                │
│ 2     outer←{                                 ⍝ Outer decoration.                │
│ 2         sizes←1 0{⊃↓(⍉⍣⍺)⍵}¨sepr⍴¨⍵         ⍝ row and col sizes of subarrays.  │
│ 2         sides←sizes/¨¨'│─'                  ⍝ vert and horiz cell sides.       │
│ 2         bords←dec↓¨'├┬'glue¨sides           ⍝ joined up outer borders.         │
│ 2         ↑,¨/('┌' '')⍺ bords'└┐'             ⍝ vertical and horizontal borders. │
│ 1     }                                                                          │
│ 1                                                                                │
│ 2     inner←{                                 ⍝ Inner subarray decorations.      │
│26         deco←{(type ⍵),1 shape ⍵}           ⍝ type and shape decorators.       │
│ 2         sepr deco¨matr dec open ⍵           ⍝ decorators: tt vv hh .           │
│ 1     }                                                                          │
│ 1                                                                                │
│ 2     collect←{                               ⍝ Collected subarrays.             │
│ 2         lft top tt vv hh←⍺                  ⍝ array and subarray decorations.  │
│ 2         cells←vv right 1 open tt hh lower ⍵ ⍝ cells boxed right and below.     │
│ 2         boxes←(dec∨0∊⍴⍵)open cells          ⍝ opened to avoid ,/⍬ problem.     │
│ 2         lft,top⍪↑⍪⌿,/boxes                  ⍝ completed collection.            │
│ 1     }                                                                          │
│ 1                                                                                │
│ 2     right←{                                 ⍝ Border right each subarray.      │
│ 2         types←2⊥¨(⍳⍴⍵)=⊂¯1+⍴⍵               ⍝ right border lower corner types. │
│ 2         chars←'┼┤┴┘'[types]                 ⍝    ..     ..      ..      chars. │
│26         rgt←{⍵,(-≢⍵)↑(≢⍵)1 1/'│',⍺}         ⍝ form right border.               │
│ 2         ((matr 1 open ⍺),¨chars)rgt¨⍵       ⍝ cells bordered right.            │
│ 1     }                                                                          │
│ 1                                                                                │
│ 2     lower←{                                 ⍝ Border below each subarray.      │
│26         bot←{⍵⍪(-1⊃⍴⍵)↑⍺ split ⍵}           ⍝ lower border.                    │
│26         split←{((¯2+1⊃⍴⍵)/'─')glue ⍺}       ⍝ decorators split with horiz line.│
│ 2         (matr↑,¨/⍺)bot¨matr ⍵               ⍝ cells bordered below.            │
│ 1     }                                                                          │
│ 1                                                                                │
│24     type←{                                  ⍝ Type decoration char.            │
│24         dec<|≡⍵:'─'                         ⍝ nested: '─'                      │
│24         isor ⍵:'∇'                          ⍝ ⎕or:    '∇'                      │
│72         sst←{                               ⍝ simple scalar type.              │
│72             0=dec×⍴⍴⍵:'─'                   ⍝ undecorated or scalar ⍕⍵: char,  │
│72             (⊃⍵∊'¯',⎕D)⊃'#~'                ⍝ otherwise, number or space ref.  │
│24         }∘⍕                                 ⍝ ⍕ distinguishes type of scalar.  │
│24         0=≡⍵:sst ⍵                          ⍝ simple scalar: type.             │
│24         {(1=⍴⍵)⊃'+'⍵}∪,sst¨dec open ⍵       ⍝ array: mixed or uniform type.    │
│ 1     }                                                                          │
│ 1                                                                                │
│26     shape←{                                 ⍝ Row and column shape decorators. │
│26         dec≤0=⍴⍴⍵:⍺/¨'│─'                   ⍝ no decoration or scalar.         │
│26         cols←(ׯ1↑⍴⍵)⊃'⊖→'                  ⍝ zero or more cols.               │
│26         rsig←(××/¯1↓⍴⍵)⊃'⌽↓'                ⍝ zero or more rows.               │
│26         rows←(¯1+3⌊⍴⍴⍵)⊃'│'rsig'⍒'          ⍝ high rank decorator overrides.   │
│26         rows cols                           ⍝ shape decorators.                │
│ 1     }                                                                          │
│ 1                                                                                │
│10     matr←{↑,↓⍵}                             ⍝ matrix from non-scalar array.    │
│ 6     sepr←{+/¨1⊂↑⍵}                          ⍝ vec-of-mats from mat-of-vecs.    │
│83     open←{16::(1⌈⍴⍵)⍴⊂'[ref]' ⋄ (⍺⌈⍴⍵)⍴⍵}   ⍝ stretched to expose nulls.       │
│51     isor←{1 ⍬≡(≡⍵)(⍴⍵)}                     ⍝ is ⎕or of object?                │
│29     glue←{0=⍴⍵:⍵ ⋄ ↑⍺{⍺,⍺⍺,⍵}/⍵}            ⍝ ⍵ interspersed with ⍺s.          │
│ 1                                                                                │
│ 1     isor ⍵:⎕FMT⊂⍵                           ⍝ simple ⎕OR: done.                │
│ 1     1=≡,⍵:⎕FMT ⍵                            ⍝ simple array: done.              │
│ 1     box ⍵                                   ⍝ recursive boxing of array.       │
│ = }                                                                              │
└──────────────────────────────────────────────────────────────────────────────────┘

    osc¨(profile'osc')⍳10           ⍝ multiple applications of subject function
┌────────────────────────────────────────────────────────┐
│77 osc←{               ⍝ Oscillate - probably returns 1.│
│77     1=⍵:1                                            │
│67     2|⍵:∇ 1+3×⍵                                      │
│50         ∇ ⍵÷2                                        │
│ = }                                                    │
└────────────────────────────────────────────────────────┘

    osc←{
        1=⍵:⍵
        2|⍵:odd ⍵
        even ⍵
    }

    odd←{
        osc 1+3×⍵
    }

    even←{
        osc ⍵÷2
    }

    P ← profile'osc' 'odd' 'even'           ⍝ profiling operator

    osc¨P ⍳100                              ⍝ profile of three functions
┌──────────────────────┐
│3242 osc←{            │
│3242         1=⍵:⍵    │
│3142         2|⍵:odd ⍵│
│2137         even ⍵   │
│   =     }            │
├──────────────────────┤
│1005 odd←{            │
│1005         osc 1+3×⍵│
│   =     }            │
├──────────────────────┤
│2137 even←{           │
│2137         osc ⍵÷2  │
│   =     }            │
└──────────────────────┘

⍝∇ profile osc subs

Back to: code

Back to: Workspaces