[cairo] Isn't there a way to pass multiple points to Cairo? (sorry for duplicate)

Andrea Canciani ranma42 at gmail.com
Sat Jul 30 11:04:22 PDT 2011

On Sat, Jul 30, 2011 at 6:04 PM, Andrej Mitrovic
<andrej.mitrovich at gmail.com> wrote:
> I was temporarily unsubscribed and this could be a duplicate post, I
> apologize if that's so.
> If I want to draw a waveform made out of 400 points and connect the
> points together to form lines (meaning point one has a line to point
> two, point two to pint three, etc.), I have to call cairo_move_to()
> and cairo_line_to() 400 times each.
> Benchmarking shows this takes ~36 milliseconds when antialiasing is
> set to off, otherwise ~60 milliseconds with antialiasing on to draw on
> my machine.

In order to find out where the time is spent, you should profile your
application. The suggestions provided below assume that most of the time
is spent rasterizing the waveform.

> Here's an example image in Cairo: http://i.imgur.com/gaMSF.jpg
> Compare that to, say, using the WinAPI Polyline() function, which
> takes an array of Points (a Point is a struct with int x,y fields). It
> takes an average of 150 microseconds, or 0.15 milliseconds to connect
> the 400 points and draw them as lines on the screen.
> Here's the image using GDI: http://i.imgur.com/x8Ln9.jpg
> I believe GDI is hardware-accelerated by default so this could account
> for why it's fast, but still I have to wonder if the massive amount of
> function calls is what is slowing down Cairo. I'm dynamically linking
> to Cairo so I can't inline any function calls.

I believe that most of the difference between cairo and Polyline()
depends on the fact that Polyline() does not stroke the path
(it probably just runs Bresenham along all the segments).

> Isn't there a function in Cairo that can take a pointer to an array of
> some {int x, y} - like structure so we could make one function call
> and paint that with cairo_stroke/cairo_paint/etc? And if not, how
> come?

You could use
but I would be quite surprised if it actually improved the performance

> I'm not too sure if it really is the amount of function calls that
> slows down the painting, or if the painting itself is slow with a
> large number of points on a path.

It's very unlikely that the slowness you're noticing depends on the
"many" calls to cairo_line_to().

You're trying to perform an operation which is known to be slow
(drawing many intersecting lines; even worse they are almost,
but not exactly, vertical).

Even if it might look weird, the same waveform rotated by 90
degrees might draw faster (depending on what the input is),
so you might want to try this.

You might also want to try stroking subpaths instead of the whole path.
This does not result in exactly the same output (especially if you're
using anti-aliasing) because pixels will be drawn multiple times
and might accumulate the color in a slightly different way, but
it might be a reasonable quality/speed compromise.

Please provide additional information (in particular cairo version
and the results of profiling) if these suggestions were not


More information about the cairo mailing list