The most important datatype in math is list. Lists in Mathematica are like set in mathematics. List is also used to hold data. In the case of large amount of data in input/output or intermediate stage, fast list operations certainly would speedup the code. One surprising fact is, seemly the same list operations may have different performance.
1. List Construction
1-0 Table is fast when creating block listThe most simple way to construct a list is use Table. Table is the suitable choice in generic cases. But making use of knowledge of the list, you can get better performance.
|Table, Array, and function apply|
1-1 use build-in functions
Some build-in functions support generation of a list. They are usually faster that explicit call them in a list constructor like Table.
|Build-in random number generator is way faster.|
1-2 use Reap-Sow, instead of Insert, Append, AppendTo, Prepend or PrependTo
1-3 Use NestList for recursable list
Recursable list means you can get the n+1-th from the n-th.
2. List Traversal
2-1. Listable attribute
Many function as Listable attribute. This means, when hitting a list, the function automatically hit on each element of the list. And the result will be the list of all hits. Using Listable functions is the fastest list traversal.
One can also Map (/@), Apply (@@, @@@), Thread, MapThread the function (head) to a list if Listable attribute is not available.
Note that some binary operations have been defined between lists and between list and scalar. This build-in operations between lists are fast and can be in parallel.
2-2. iterate a list
If one has to explicitly traverse a list, Mathematica allows iterating a list directly. It is faster than using iterators. The iteration concept is not limited to list. In Do and other iterating cases, list can also be iterated directly.
2-3. use build-in list manipulation
Mathematica has defined many build-in list operations. Using these build-in operations are generally faster.