Code Optimisation: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
mNo edit summary |
||
Line 16: | Line 16: | ||
*_y=0; while {_y = _y + 1; _y < count [0,0,0,0]} do { ... }; | *_y=0; while {_y = _y + 1; _y < count [0,0,0,0]} do { ... }; | ||
*for [{_y=0},{_y<(count [0,0,0,0])},{_y=_y+1}] do { ... } | *for [{_y=0},{_y<(count [0,0,0,0])},{_y=_y+1}] do { ... } | ||
==Avoid O(n^2)!!== | |||
Commonly you may set up foreach foreach's. | |||
'For' example: | |||
*{ { ... } foreach [0,0,0]; } foreach [0,0,0]; | |||
This example is of the order (n^2) (3^2 = 9 iterations). For each element you add to this, it will increase the processing exponentially. Which can lead to poor performance in snap functions. | |||
==Avoiding the 0.3ms delay== | |||
Many times you may want to avoid the '''0.3 ms''' delay (not 3ms!), and there a few tricks around how to do it... |
Revision as of 21:46, 20 January 2010
Adding elements to an array
- set is 56x faster than binary addition (_a set [count _a,_v] vs _a = _a + [_v])
Getting position
Loops
These first two loop types are identical in speed (+/- 10%), and are more than 2x as fast the proceeding two loop types.
- "_y" from 0 to (count [0,0,0,0] - 1) step 1 do { ... };
- { ... } foreach [0,0,0,0];
Where as these two loops are much slower, and for maximum performance, avoided.
- _y=0; while {_y = _y + 1; _y < count [0,0,0,0]} do { ... };
- for [{_y=0},{_y<(count [0,0,0,0])},{_y=_y+1}] do { ... }
Avoid O(n^2)!!
Commonly you may set up foreach foreach's. 'For' example:
- { { ... } foreach [0,0,0]; } foreach [0,0,0];
This example is of the order (n^2) (3^2 = 9 iterations). For each element you add to this, it will increase the processing exponentially. Which can lead to poor performance in snap functions.
Avoiding the 0.3ms delay
Many times you may want to avoid the 0.3 ms delay (not 3ms!), and there a few tricks around how to do it...