10 Benchmarking and profiling

 

This chapter covers

  • Benchmarking code in Haskell
  • Exploring time and space consumption
  • Tips and tricks to optimize code

In this chapter, we’ll discuss how to check the performance of our code. As usual, we are interested in time and space usage characteristics. We’ll discuss how to compare different implementations of the same function in terms of execution time using benchmarking and choose the most suitable implementation. We’ll explore time and space consumption of our programs (we call it profiling) and use this information to find drawbacks in the implementation, such as space leaks. Finally, we’ll see how to use benchmarking and profiling to make our programs run faster.

Remember, we’ve already implemented the first version of the IP filtering application and tested it intensively. We’ll continue working on the same project in this chapter. Let’s make the following assumptions on how this utility program is intended to be used:

  • The list of ranges might be rather large, containing thousands of them.
  • The range database is fixed at run time, so we don’t have to reload it.
  • IP addresses for filtering are processed as they go; hence, there might be millions of them.

10.1 Benchmarking functions with criterion

10.1.1 Benchmarking implementations of a simple function

10.1.2 Benchmarking an IPv4 filtering application

10.2 Profiling execution time and memory usage

10.2.1 Simulating iplookup usage in the real world

10.2.2 Analyzing execution time and memory allocation

10.2.3 Analyzing memory usage

10.3 Tuning performance of the IPv4 filtering application

10.3.1 Choosing the right data structure

10.3.2 Squeezing parseIP performance

Summary