title: Writing a time function in Haskell
body: <p>I’m new to Haskell and I’d like to be able to time the runtime of a given function call or snippet of code.</p>
  
  <p>In Clojure I can use ‘<a href="http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/time" rel="noreferrer">time</a>’:</p>
  
  <pre><code>user=&gt; (time (apply * (range 2 10000)))
  "Elapsed time: 289.795 msecs"
  2846259680917054518906413212119868890148051...
  </code></pre>
  
  <p>In Scala, I can define the function myself:</p>
  
  <pre><code>scala&gt; def time[T](code : =&gt; T) =  {
       |   val t0 = System.nanoTime : Double
       |   val res = code
       |   val t1 = System.nanoTime : Double
       |   println("Elapsed time " + (t1 - t0) / 1000000.0 + " msecs")
       |   res
       | }
  time: [T](=&gt; T)T
  
  scala&gt; time((1 to 10000).foldLeft(1:BigInt)(_*_))
  Elapsed time 274.292224 msecs
  res0: BigInt = 284625968091705451...
  </code></pre>
  
  <p>How can I write the equivalent of my Scala function or Clojure's ‘time’ in Haskell? The <a href="http://hackage.haskell.org/package/timeit" rel="noreferrer">System.TimeIt module</a> I've found on Hackage is not general enough because it works only if an IO computation is being measured. So <code>timeIt(4 + 4)</code> wouldn't work, only <code>timeIt(print $ 4 + 4)</code>, which gets annoying fast. Beside, I really want to see how Haskell handles the general case.</p>
  
  <p>Thank you!</p>