Re: Speed of Quartz (was: optimizing compilers)
Re: Speed of Quartz (was: optimizing compilers)
- Subject: Re: Speed of Quartz (was: optimizing compilers)
- From: "Erik M. Buck" <email@hidden>
- Date: Mon, 4 Feb 2002 15:04:39 -0600
Here is the test using Core Graphics calls instead of NSBezierPath:
Once again, the second example should be slower but is in fact ten times
faster. The problem with complex paths is Quartz and not NSBezierPath.
- (void)drawRect:(NSRect)aRect
{
CGContextRef cgContext = [[NSGraphicsContext currentContext]
graphicsPort];
int i;
NSAssert(MYNUM_POINTS >= 2, @"Compiled with MYNUM_POINTS < 2");
[[NSColor whiteColor] set];
[NSBezierPath fillRect:[self bounds]];
for(i = 0; i < MYNUM_POINTS; i++)
{
_myPoints[i] = NSMakePoint(random() % (int)[self bounds].size.width,
random() % (int)[self bounds].size.height);
}
[[NSColor greenColor] set];
// Construct path
CGContextMoveToPoint(cgContext, _myPoints[0].x, _myPoints[0].y);
for(i = 1; i < MYNUM_POINTS; i++)
{
CGContextAddLineToPoint(cgContext, _myPoints[i].x, _myPoints[i].y);
}
// Stroke
CGContextSetLineWidth(cgContext, 1.0);
CGContextSetLineCap(cgContext, kCGLineCapRound);
CGContextSetLineJoin(cgContext, kCGLineJoinRound);
CGContextSetMiterLimit(cgContext, 4.0);
CGContextDrawPath(cgContext, kCGPathStroke);
}
// This one is 10 times faster
- (void)drawRect:(NSRect)aRect
{
CGContextRef cgContext = [[NSGraphicsContext currentContext]
graphicsPort];
int i;
NSAssert(MYNUM_POINTS >= 2, @"Compiled with MYNUM_POINTS < 2");
[[NSColor whiteColor] set];
[NSBezierPath fillRect:[self bounds]];
for(i = 0; i < MYNUM_POINTS; i++)
{
_myPoints[i] = NSMakePoint(random() % (int)[self bounds].size.width,
random() % (int)[self bounds].size.height);
}
[[NSColor greenColor] set];
// Construct and stroke path path
for(i = 1; i < MYNUM_POINTS; i++)
{
CGContextBeginPath(cgContext);
CGContextMoveToPoint(cgContext, _myPoints[i-1].x, _myPoints[i-1].y);
CGContextAddLineToPoint(cgContext, _myPoints[i].x, _myPoints[i].y);
CGContextSetLineWidth(cgContext, 1.0);
CGContextSetLineCap(cgContext, kCGLineCapRound);
CGContextSetLineJoin(cgContext, kCGLineJoinRound);
CGContextSetMiterLimit(cgContext, 4.0);
CGContextDrawPath(cgContext, kCGPathStroke);
}
}
----- Original Message -----