chapter ten

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 see how to check the performance of our code. As usual, we’ll be 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 which implementation is the most suitable. 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 some assumptions on how this utility program is intended to be used:

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

10.1  Benchmarking functions with criterion

10.1.1  Cooking cabal-projects for benchmarking

10.1.2  Benchmarking 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 speed 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

10.4  Summary