weigh

Measure allocations of a Haskell functions/values

https://github.com/fpco/weigh#readme

Version on this page:0.0.7
LTS Haskell 22.14:0.0.17
Stackage Nightly 2024-03-29:0.0.17
Latest on Hackage:0.0.17

See all snapshots weigh appears in

BSD-3-Clause licensed by Chris Done
Maintained by [email protected]
This version can be pinned in stack with:weigh-0.0.7@sha256:a208d7f44eefea3e5d178179f7e8419ef77b3d858e8b2d1fc933cf9e9d7eeec6,1621

Module documentation for 0.0.7

weigh Build Status

Measures the memory usage of a Haskell value or function

Example use

import Weigh

-- | Weigh integers.
main :: IO ()
main =
  mainWith (do integers
               ints)

-- | Just counting integers.
integers :: Weigh ()
integers =
  do func "integers count 0" count 0
     func "integers count 1" count 1
     func "integers count 2" count 2
     func "integers count 3" count 3
     func "integers count 10" count 10
     func "integers count 100" count 100
  where count :: Integer -> ()
        count 0 = ()
        count a = count (a - 1)

-- | We count ints and ensure that the allocations are optimized away
-- to only two 64-bit Ints (16 bytes).
ints :: Weigh ()
ints =
  do validateFunc "ints count 1" count 1 (maxAllocs 24)
     validateFunc "ints count 10" count 10 (maxAllocs 24)
     validateFunc "ints count 1000000" count 1000000 (maxAllocs 24)
  where count :: Int -> ()
        count 0 = ()
        count a = count (a - 1)

Output results:

Case                Bytes  GCs  Check
integers count 0        0    0  OK
integers count 1       32    0  OK
integers count 2       64    0  OK
integers count 3       96    0  OK
integers count 10     320    0  OK
integers count 100  3,200    0  OK
ints count 1            0    0  OK
ints count 10           0    0  OK
ints count 1000000      0    0  OK

You can try this out with stack test in the weight directory.

To try out other examples, try:

  • stack test :weigh-maps --flag weigh:weigh-maps