Haskell Benchmarking with Counts
Benchmarking with counts measures how much work a program does by counting items and results. It compares workloads by their counts instead of by clock time.
A count and a sum prove the workload
This run processes 100 numbers. The printed count and sum are the benchmark result.
import Data.List (foldl')
main :: IO ()
main = do
let xs = [1..100]
print (length xs)
print (foldl' (+) 0 xs)A filtered count measures a smaller workload
This run counts only the even numbers from 1 to 20. The count is 10, and the sum of those numbers is 110.
main :: IO ()
main = do
let evens = filter even [1..20]
print (length evens)
print (sum evens)A larger input gives a larger count
This run processes 1000 numbers. Its sum grows with the input size.
import Data.List (foldl')
main :: IO ()
main = do
let xs = [1..1000]
print (length xs)
print (foldl' (+) 0 xs)Example
import Data.List (foldl')
main :: IO ()
main = do
print (length [1..10], foldl' (+) 0 [1..10])
print (length [1..100], foldl' (+) 0 [1..100])
print (length [1..1000], foldl' (+) 0 [1..1000])Key points
- Count: the number of items a program processes in one run.
- Result: the value a counted run produces, such as a sum.
- Use foldl' to add a list strictly so no tower of unevaluated additions builds up.
- Use a larger input to test scaling and compare its count with the smaller input.
- Use the same program for each size so only the input changes.
In this example, the program runs the same strict sum on three sizes. It prints the count and the sum for 10 numbers, then for 100 numbers, then for 1000 numbers, so the reader can compare the workloads by their counts.
Note
A plain run prints counts and results only, never a duration.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Haskell Playground
Exercise
A benchmark run processes [1..100] with foldl' (+) 0. What does it print as its count and sum?