Hello, <div><br></div><div>I want to write a timing method to test sub steps in my program.  I tested my timing method as follows:</div><div><br></div><div>=====</div><div>time.hs</div><div>=====</div><div><br></div><div><div>
<div>import Data.Time</div><div><br></div><div>time :: String -&gt; IO a -&gt; IO a</div><div>time str a = do</div><div>    start &lt;- getCurrentTime</div><div>    v &lt;- a</div><div>    end &lt;- getCurrentTime</div><div>
    let diff = diffUTCTime end  start</div><div>    putStrLn $ str ++ showNominalDiffTime (diff)</div><div>    return v</div><div><br></div><div>showNominalDiffTime :: NominalDiffTime -&gt; String</div><div>showNominalDiffTime nTime = let timeString = show nTime in</div>
<div>                              take (length timeString - 1) timeString ++ &quot; sec&quot;</div><div><br></div><div>fun :: IO (Int, [Int])</div><div>fun = do</div><div>   i &lt;- time &quot;product: &quot; $ return $! product [1..10000] -- testing strictness : evaluated</div>
<div>   j &lt;- time &quot;product list: &quot; $ return $! map (\_ -&gt; product [1..10000]) [1..3] -- testing strictness : not evaluated  List is in hnf</div><div>   return (i, j)</div><div><br></div><div>main =  fun</div>
</div></div><div><br></div><div>=====</div><div>In the shell</div><div>=====</div><div><div>$ time ./time</div><div>product: 0.001813 sec</div><div>product list: 0.000001 sec</div><div><br></div><div>real<span class="Apple-tab-span" style="white-space:pre">        </span>0m0.006s</div>
<div>user<span class="Apple-tab-span" style="white-space:pre">        </span>0m0.000s</div><div>sys<span class="Apple-tab-span" style="white-space:pre">        </span>0m0.005s</div></div><div><br></div><div>Notice that the ($!) operation will reduce a term to head normal form (hnf), not normal form.</div>
<div> </div><div>In the &quot;product list&quot; testing, the &quot;complicated&quot; map evaluation is reduced to a list of redex.  Therefore, the result does not reflect the time needed to get the final value.</div><div>
<br></div><div>How could I improve my timing method so that it will give the time to reduce a general expression to normal form?</div><div><br></div><div>Jiansen</div><meta http-equiv="content-type" content="text/html; charset=utf-8">