Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way of attaching "Set Cost Center" to a Monad (Haskell)

I'm trying to profile my program with SCC (Set Cost Center) annotations. What's the best way to find out how long it takes for a monad defined by a do statement to run? (It is illegal to put an SCC statement in a do block.) In other words, let's say I have:

do
  x <- computeStuff
  y <- computeStuff
  return (x + y)

How do I find the total execution time for the two computeStuff and the x + y (instead of the construction time of the Monad)?

like image 962
yong Avatar asked Oct 20 '22 15:10

yong


1 Answers

SCCs can be set on any expression. (do { ... }) is a valid expression. So is computeStuff and return (x + y). The only thing that isn't an expression here is x <- computeStuff. You say you want the total time, which I understand to mean the total time for the entire do block. But you can place SCCs anywhere inside the do block; for example, the following is perfectly valid.

computeStuff :: IO Int 
computeStuff = return 0 

test = {-# SCC "total" #-} (
  do
    x <- {-# SCC "x" #-} computeStuff
    y <- {-# SCC "y" #-} computeStuff
    return $ {-# SCC "x+y" #-} (x + y) 
  )
like image 150
user2407038 Avatar answered Oct 22 '22 23:10

user2407038