Skip to main content

Why is UIBezierPath faster than Core Graphics path?



I was playing around with drawing paths, and I noticed that in at least some cases, UIBezierPath outperforms what I thought would be a Core Graphics equivalent. The -drawRect: method below creates two paths: one UIBezierPath, and one CGPath. The paths are identical except for their locations, but stroking the CGPath takes roughly twice as long as stroking the UIBezierPath.







- (void)drawRect:(CGRect)rect

{

CGContextRef ctx = UIGraphicsGetCurrentContext();



// Create the two paths, cgpath and uipath.

CGMutablePathRef cgpath = CGPathCreateMutable();

CGPathMoveToPoint(cgpath, NULL, 0, 100);



UIBezierPath *uipath = [[UIBezierPath alloc] init];

[uipath moveToPoint:CGPointMake(0, 200)];



// Add 200 curve segments to each path.

int iterations = 200;

CGFloat cgBaseline = 100;

CGFloat uiBaseline = 200;

CGFloat xincrement = self.bounds.size.width / iterations;

for (CGFloat x1 = 0, x2 = xincrement;

x2 < self.bounds.size.width;

x1 = x2, x2 += xincrement)

{

CGPathAddCurveToPoint(cgpath, NULL, x1, cgBaseline-50, x2, cgBaseline+50, x2, cgBaseline);

[uipath addCurveToPoint:CGPointMake(x2, uiBaseline)

controlPoint1:CGPointMake(x1, uiBaseline-50)

controlPoint2:CGPointMake(x2, uiBaseline+50)];

}

[[UIColor blackColor] setStroke];

CGContextAddPath(ctx, cgpath);



// Stroke each path.

[self strokeContext:ctx];

[self strokeUIBezierPath:uipath];



[uipath release];

CGPathRelease(cgpath);

}



- (void)strokeContext:(CGContextRef)context

{

CGContextStrokePath(context);

}



- (void)strokeUIBezierPath:(UIBezierPath*)path

{

[path stroke];

}







Both paths use CGContextStrokePath(), so I created separate methods to stroke each path so that I can see the time used by each path in Instruments. Below are typical results (call tree inverted); you can see that -strokeContext: takes 9.5 sec., while -strokeUIBezierPath: takes only 5 sec.:







Running (Self) Symbol Name

14638.0ms 88.2% CGContextStrokePath

9587.0ms 57.8% -[QuartzTestView strokeContext:]

5051.0ms 30.4% -[UIBezierPath stroke]

5051.0ms 30.4% -[QuartzTestView strokeUIBezierPath:]







It looks like UIBezierPath is somehow optimizing the path that it creates, or I'm creating the CGPath in a naïve way. What can I do to speed up my CGPath drawing?


Comments

  1. You are correct in that UIBezierPath is simply an objective-c wrapper for Core Graphics, and therefore will perform comparably. The difference (and reason for your performance delta) is your CGContext state when drawing your CGPath directly is quite different to that setup by UIBezierPath. If you look at UIBezierPath, it has settings for:


    lineWidth,
    lineJoinStyle,
    lineCapStyle,
    miterLimit and
    flatness


    When examining the call (disassembly) to [path stroke], you will note that it configures the current graphic context based on those previous values before performing the CGContextStrokePath call. If you do the same prior to drawing your CGPath, it will perform the same:

    - (void)drawRect:(CGRect)rect
    {
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // Create the two paths, cgpath and uipath.
    CGMutablePathRef cgpath = CGPathCreateMutable();
    CGPathMoveToPoint(cgpath, NULL, 0, 100);

    UIBezierPath *uipath = [[UIBezierPath alloc] init];
    [uipath moveToPoint:CGPointMake(0, 200)];

    // Add 200 curve segments to each path.
    int iterations = 80000;
    CGFloat cgBaseline = 100;
    CGFloat uiBaseline = 200;
    CGFloat xincrement = self.bounds.size.width / iterations;
    for (CGFloat x1 = 0, x2 = xincrement;
    x2 < self.bounds.size.width;
    x1 = x2, x2 += xincrement)
    {
    CGPathAddCurveToPoint(cgpath, NULL, x1, cgBaseline-50, x2, cgBaseline+50, x2, cgBaseline);
    [uipath addCurveToPoint:CGPointMake(x2, uiBaseline)
    controlPoint1:CGPointMake(x1, uiBaseline-50)
    controlPoint2:CGPointMake(x2, uiBaseline+50)];
    }
    [[UIColor blackColor] setStroke];
    CGContextAddPath(ctx, cgpath);

    // Stroke each path
    CGContextSaveGState(ctx); {
    // configure context the same as uipath
    CGContextSetLineWidth(ctx, uipath.lineWidth);
    CGContextSetLineJoin(ctx, uipath.lineJoinStyle);
    CGContextSetLineCap(ctx, uipath.lineCapStyle);
    CGContextSetMiterLimit(ctx, uipath.miterLimit);
    CGContextSetFlatness(ctx, uipath.flatness);
    [self strokeContext:ctx];
    CGContextRestoreGState(ctx);
    }
    [self strokeUIBezierPath:uipath];

    [uipath release];
    CGPathRelease(cgpath);
    }

    - (void)strokeContext:(CGContextRef)context
    {
    CGContextStrokePath(context);
    }

    - (void)strokeUIBezierPath:(UIBezierPath*)path
    {
    [path stroke];
    }


    Snapshot from Instruments:

    ReplyDelete
  2. From Apple's Drawing and Printing Guide for iOS :


    For creating paths in iOS, it is
    recommended that you use UIBezierPath
    instead of CGPath functions unless you
    need some of the capabilities that
    only Core Graphics provides, such as
    adding ellipses to paths. For more on
    creating and rendering paths in UIKit,
    see “Drawing Shapes Using Bezier
    Paths.”


    I can only guess that UIBezierPath is more efficient when working on the iOS's smaller screen/resolution, by doing some approximations , instead of doing all the hard work for possible larger (and higher resolution) Apple cinema displays.

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?

Java Urban Myths

Along the line of C++ Urban Myths and Perl Myths : What are the Java Urban Myths? That is, the ideas and conceptions about Java that are common but have no actual roots in reality . As a Java programmer, what ideas held by your fellow Java programmers have you had to disprove so often that you've come to believe they all learned at the feet of the same drunk old story-teller? Ideally, you would express these myths in a single sentence, and include an explanation of why they are false.

CCNA 1 Final Exam 2011 latest (hot hot hot)

  Hi! I have been posted content of ccna1 final exam (latest and only question.) I will post the answer and insert image on sunday. If you care, please subscribe your email an become a first person have full test content. Subcribe now  Some question  have not content because this question have images content. So that can you wait for me? SUNDAY 1. A user sees the command prompt: Router(config-if)# . What task can be performed at this mode? Reload the device. Perform basic tests. Configure individual interfaces. Configure individual terminal lines. 2. Refer to the exhibit. Host A attempts to establish a TCP/IP session with host C. During this attempt, a frame was captured with the source MAC address 0050.7320.D632 and the destination MAC address 0030.8517.44C4. The packet inside the captured frame has an IP source address 192.168.7.5, and the destination IP address is 192.168.219.24. At which point in the network was this packet captured? leaving host A leaving ATL leaving...