Eric Christensen
Eric Christensen

Reputation: 839

How to optimize quartz 2d?

I have a block of code that's essentially:

    for(int i=0;i<aInt;i++){
        CGPoint points[2] = {CGPointMake(i,0),CGPointMake(i,bArray[i])};
        CGContextStrokeLineSegments(myContext, points, 2);
    }

which is causing a bit of a bottleneck when aInt gets large, as it's likely to do in my case. I don't know enough about quartz 2d to know how to best optimize this. Is it better to create a single huge point array in the loop and then stoke the entire array once?

Or more ideally, I've just optimized a different part of code dealing with arrays. In doing so, I converted to using C-style arrays which sped things up tremendously. Is there a similar low-level way of doing the above?

Thanks!

Upvotes: 2

Views: 848

Answers (2)

epatel
epatel

Reputation: 46051

I also imagine making a large array will make it faster. There will definitely be less calls into CGContextStrokeLineSegments.

CGPoint *points = (CGPoint*)malloc(sizeof(CGPoint)*aInt*2);

for(int i=0;i<aInt;i++){
    points[i*2] = CGPointMake(i,0);
    points[i*2+1] = CGPointMake(i,bArray[i]));
}

CGContextStrokeLineSegments(myContext, points, aInt*2);

free(points);

Upvotes: 3

Ben Gottlieb
Ben Gottlieb

Reputation: 85532

Yes, creating a single large array will definitely be faster than stroking each individual line segment.

Upvotes: 0

Related Questions