Thanks to advances in computer technology, performance is no longer a concern for the majority of programs. However, many applications exist, particularly in the context of scientific computing, where performance is still important. In such cases, programs can be optimized to run faster by exploiting knowledge about the relative performance of different approaches.

This chapter examines the most important techniques for optimizing F# programs. The overall approach to whole program optimization is to perform each of the following steps in order:

  1. Profile the program compiled with automated optimizations and running on representative input.

  2. Of the sequential computations performed by the program, identify the most time-consuming one from the profile.

  3. Calculate the (possibly asymptotic) algorithmic complexity of this bottleneck in terms of suitable primitive operations.

  4. If possible, manually alter the program such that the algorithm used by the bottleneck has a lower asymptotic complexity and repeat from step 1.

  5. If possible, modify the bottleneck algorithm such that it accesses its data structures less randomly to increase cache coherence.

  6. Perform low-level optimizations on the expressions in the bottleneck.

The mathematical concept of asympotic algorithmic complexity (covered in section 3.1) is an excellent way to choose a data structure when inputs will be large, i.e. when the asymptotic approximation is most accurate. However, many programs perform a large number ...

Get F# for Scientists now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.