Sharpening your APL knife
Performance
General Rules
- Use idioms whenever possible
- Note that what is fast today may be slow tomorrow!
- Worse: what is fast on one processor may be slow on another
- New versions should pass automated tests for performance issues
- Use ⎕MONITOR, and use it often
About Data
- Select only as much data as needed
- Reduce the data whenever possible
- bool⌿RelationalTable
- Make the data structure fit your needs
- 7⊃¨↓Matrix ⍝ get column 7
About Each -1-
- Be careful with the "Each" operator
- Several eaches on a single line: be on the alert!
- Depending on the operation, other techniques can be faster, sometimes much faster
About Each -2-
If you know or assume that a particular operation might be or become a performance bottleneck:
- :For loop
- Assigned function
- Dynamic function
- Use the defined "Each" operator if the data contains lots of copies
Practise a.1
- Given: a numeric matrix with n rows and two columns
- The matrix is sorted by the first column
- We are looking for a function that adds up all values in the second column for all rows which equals in the first column
Practise b.2
- 10 1
- 10 3
- 10 4
- 11 10
- 11 20
- ⍝ result:
- 10 8
- 11 30
Practise b.1
- ⍝ Given a boolean vector:
- 0 0 0 1 0 0 1 1 1 1 0 0 0 0 1 0 1 1
- ⍝ Wanted as result:
- 0 0 0 1 0 0 2 2 2 2 0 0 0 0 3 0 4 4
End