Chapter 11
Optimizing Your Algorithms

Thus far, you haven’t needed to worry too much about the performance of the algorithms you’ve implemented. The solutions have been small, and you’ve been working with relatively small populations. Because of this, you haven’t needed to be concerned with efficiency. In the real world, you’ll often need to deal with significantly larger solutions and populations when applying genetic algorithms to practical problems.

As it turns out, Elixir is a language that wasn’t designed to be extremely efficient at computationally expensive tasks. Things like floating-point math and matrix multiplication are slow in Elixir. Elixir runs on the BEAM, which was designed for telecommunication systems. This doesn’t mean you can’t write performant genetic algorithms in Elixir; it means you need to be deliberate in optimizing those algorithms.

The BEAM is the Erlang virtual machine. It’s the core of Erlang/OTP. Every Elixir file compiles to BEAM bytecode. The best way to understand the performance of your applications is to understand what happens when your application compiles and runs the BEAM bytecode. This chapter will briefly cover some aspects of the BEAM; however, the material isn’t comprehensive by any means. For a fantastic explanation of the internals of the BEAM, check out The BEAM Book.[14]

In this chapter, you’ll learn how to benchmark and profile your algorithms, and you’ll briefly learn about where performance really matters. Additionally, you’ll learn different ways to optimize your algorithms. We’ll walk through a series of optimizations, in the following order:

  1. Creating benchmarks and profiling your algorithms.
  2. Optimizing the performance of Elixir code.
  3. Parallelizing your algorithms.
  4. Writing NIFS.

When working through optimizations, this is generally the order of optimizations you should make—only progressing to the next step when absolutely necessary.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset