<HTML>
<HEAD>
<TITLE>Re: [Haskell-cafe] Efficient Factoring Out of Constants</TITLE>
</HEAD>
<BODY>
<FONT SIZE="4"><FONT FACE="Calibri, Verdana, Helvetica, Arial"><SPAN STYLE='font-size:11pt'>On 17/01/2009 16:55, &quot;Luke Palmer&quot; &lt;lrpalmer@gmail.com&gt; wrote:<BR>
Wow. &nbsp;I strongly suggest you forget about efficiency completely and become a proficient high-level haskeller, and then dive back in. &nbsp;Laziness changes many runtime properties, and renders your old ways of thinking about efficiency almost useless.<BR>
<BR>
If you are interested, though, you can use the ghc-core tool on hackage to look at the core (lowish-level intermediate language) and even the generated assembly for minimal cases. &nbsp;It's dense, but interesting if you have the time to study it.<BR>
<BR>
Others will know more about this specific speculation than I.<BR>
<BR>
Luke<BR>
<BR>
[Phil]<BR>
Heh heh &#8211; totally accept what your saying, I am obsessing over details here. &nbsp;I&#8217;ve ran some empirical tests to get some crude insight (just using the linux&#8217;s time program), &nbsp;I expected the differences to be small for the amount of data I was passing around, but I was surprised. &nbsp;I modified the code ever so slightly to use data structures to pass around the data. &nbsp;Thus got rid of the getSum and getStateInfo functions. &nbsp;In the first unfactored example everything was passed around in one structure. &nbsp;In the second example I had two structures; one for constant data and one for state date. &nbsp;Thus doesn&#8217;t really change the problem it just means that in the unfactored example mcSimulate takes one parameter (holding data constant and variable state data) and in the factored example it takes two parameters. &nbsp;The rest of the code remained more-or-less identical to my original post.<BR>
<BR>
Running both programs at the same time on an otherwise unloaded CPU gave a very consistent result over numerous trials that for 1 million calls to mcSimulate, the unfactored example took approx 1m44s and the factored example took 1m38 &#8211; which is a fairly significant difference.<BR>
<BR>
So whilst I can&#8217;t offer any exact explanation, it is clear that factoring out even a few parameters taking up little memory produces a significant performance increase. &nbsp;This would suggest that my &#8216;JMP&#8217; analysis is not right and that the compiler is able to optimize the factored version better.<BR>
<BR>
If anyone else fancies offering up their 2 cents on what the compiler is doing, I&#8217;d still be interested, but the empirical evidence alone is enough for me to be swayed to factoring all static parameters in an iteration out of the iteration and into a wrapper.<BR>
<BR>
Phil.<BR>
<BR>
<BR>
</SPAN></FONT></FONT>
</BODY>
</HTML>