I forgot to specify my environment.<div><br></div><div>Windows Server 2008 R2 x64, ghc 7.0.3.</div><div><br></div><div>However, I observed the same speed differences on a 64-bit ubuntu with ghc 6.12 - I profiled my application with cairo-trace, and cairo-perf-trace drew in a fraction of a second the picture that my Haskell program spend a dozen seconds drawing.<br>
<br><div class="gmail_quote">On Wed, Nov 2, 2011 at 1:17 PM, Eugene Kirpichov <span dir="ltr"><<a href="mailto:ekirpichov@gmail.com">ekirpichov@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
Hello,<br><br>I've got two very simple programs that draw a very simple picture using cairo, doing a couple hundred thousand of cairo calls.<br>One program is in C++. The other is in Haskell and uses the cairo library bindings.<br>
<br>The C++ program completes in a fraction of a second, the Haskell program takes about 7-8 seconds to run. They produce exactly the same output.<br><br>What could be at fault here? Why are the cairo bindings working so slow? (I suppose there isn't too much cairo-specific stuff here, perhaps it's a general FFI question?)<div>
<br></div><div>#include "cairo.h"<br>int main() {<br> cairo_surface_t *surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1024, 768);<br> cairo_t *cr = cairo_create(surface);<br> cairo_set_source_rgb(cr, 0, 255, 0);<br>
for(int x = 0; x < 1024; x += 2) for(int y = 0; y < 768; y += 2) {<br> cairo_rectangle(cr, x, y, 1, 1);<br> cairo_fill(cr);<br> }<br> cairo_surface_write_to_png(surface, "picture.png");<br>
return 0;<br>}<br><br>module Main where<br><br>import qualified Graphics.Rendering.Cairo as C<br>import Control.Monad<br><br>main = C.withImageSurface C.FormatARGB32 1024 768 $ \s -> do<br> C.renderWith s $ do<br>
C.setSourceRGBA 0 255 0 255<br> forM_ [0,2..1024] $ \x -> do<br> forM_ [0,2..768] $ \y -> do<br> C.rectangle x y 1 1<br> C.fill<br> C.surfaceWriteToPNG s "picture.png"<span class="HOEnZb"><font color="#888888"><br>
<br>-- <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><br></font></span></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>