Oh. This is pretty crazy, I wonder what they&#39;re doing with GMP so much...<div><br></div><div>I modified the Haskell program to use cairo directly, even with safe calls, and it now takes the same time as the C program.</div>
<div><br></div><div><div>{-# LANGUAGE ForeignFunctionInterface #-}</div><div>module Main where</div><div><br></div><div>import qualified Graphics.Rendering.Cairo as C</div><div>import Control.Monad</div><div>import Foreign</div>
<div>import Foreign.C.Types</div><div>import Foreign.C.String</div><div><br></div><div>foreign import ccall &quot;cairo.h cairo_image_surface_create&quot; cairo_image_surface_create :: CInt -&gt; CInt -&gt; CInt -&gt; IO (Ptr ())</div>
<div>foreign import ccall &quot;cairo.h cairo_create&quot; cairo_create :: Ptr () -&gt; IO (Ptr ())</div><div>foreign import ccall &quot;cairo.h cairo_set_source_rgb&quot; cairo_set_source_rgb :: Ptr () -&gt; CDouble -&gt; CDouble -&gt; CDouble -&gt; IO ()</div>
<div>foreign import ccall &quot;cairo.h cairo_rectangle&quot; cairo_rectangle :: Ptr () -&gt; CDouble -&gt; CDouble -&gt; CDouble -&gt; CDouble -&gt; IO ()</div><div>foreign import ccall &quot;cairo.h cairo_fill&quot; cairo_fill :: Ptr () -&gt; IO ()</div>
<div>foreign import ccall &quot;cairo.h cairo_surface_write_to_png&quot; cairo_surface_write_to_png :: Ptr () -&gt; CString -&gt; IO ()</div><div><br></div><div>main = do</div><div>  s &lt;- cairo_image_surface_create 0 1024 768 </div>
<div>  cr &lt;- cairo_create s</div><div>  cairo_set_source_rgb cr 0 255 0</div><div>  forM_ [0,2..1024] $ \x -&gt; do</div><div>    forM_ [0,2..768] $ \y -&gt; do</div><div>      cairo_rectangle cr x y 1 1</div><div>      cairo_fill cr</div>
<div>  pic &lt;- newCString &quot;picture.png&quot;</div><div>  cairo_surface_write_to_png s pic</div><div><br></div><div class="gmail_quote">On Wed, Nov 2, 2011 at 1:58 PM, Vincent Hanquez <span dir="ltr">&lt;<a href="mailto:tab@snarc.org">tab@snarc.org</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div class="im">On 11/02/2011 09:51 AM, Eugene Kirpichov wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Claude,<br>
<br>
I suspected that the issue could be about unsafe foreign imports - all imports in the cairo bindings are &quot;safe&quot;.<br>
I compiled myself a version of cairo bindings with the &quot;rectangle&quot; and &quot;fill&quot; functions marked as unsafe.<br>
<br>
Unfortunately that didn&#39;t help the case at all, even though the core changed FFI calls from &quot;__pkg_ccall_GC&quot; to &quot;__pkg_ccall&quot;. The performance stayed the same; the overhead is elsewhere.<br>
<br>
</blockquote></div>
doing a ltrace, i think the reason is pretty obvious, there&#39;s a lot of GMP calls:<br>
<br>
__gmpz_init(0x7f5043171730, 1, 0x7f5043171750, 0x7f5043171740, 0x7f50431d2508) = 0x7f50431d2530<br>
__gmpz_mul(0x7f5043171730, 0x7f5043171750, 0x7f5043171740, 0x7f50431d2538, 0x7f50431d2508) = 1<br>
__gmpz_init(0x7f5043171728, 1, 0x7f5043171748, 0x7f5043171738, 0x7f50431d2538) = 0x7f50431d2568<br>
__gmpz_mul(0x7f5043171728, 0x7f5043171748, 0x7f5043171738, 0x7f50431d2570, 0x7f50431d2538) = 1<br>
__gmpn_gcd_1(0x7f50431d2580, 1, 1, 1, 1)     = 1<br>
&lt;repeated thousand of time&gt;<br>
<br>
before each call cairo calls.<br>
<br>
just to make sure, the C version doesn&#39;t exhibit this behavior.<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
Vincent</font></span><div class="HOEnZb"><div class="h5"><br>
<br>
______________________________<u></u>_________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br>Eugene Kirpichov<br>Principal Engineer, Mirantis Inc. <a href="http://www.mirantis.com/" target="_blank">http://www.mirantis.com/</a><br>Editor, <a href="http://fprog.ru/" target="_blank">http://fprog.ru/</a><br>

</div>