views:

226

answers:

2

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!

A: 

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

Ben Gottlieb
+3  A: 

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);
epatel
Thanks. Using something similar to this code cut the time of the loop by 75%. I'd still like it faster, but this is a big improvement, so I'll take it for now.
Eric Christensen