title: Performance when frequently drawing CGPaths
body: <p>I am working on an iOS App that visualizes data as a line-graph. The graph is drawn as a <code>CGPath</code> in a fullscreen custom <code>UIView</code> and contains at most 320 data-points. The data is frequently updated and the graph needs to be redrawn accordingly – a refresh rate of 10/sec would be nice.</p>
  
  <p>So far so easy. It seems however, that my approach takes a lot of CPU time. Refreshing the graph with 320 segments at 10 times per second results in 45% CPU load for the process on an iPhone 4S.</p>
  
  <p>Maybe I underestimate the graphics-work under the hood, but to me the CPU load seems a lot for that task.</p>
  
  <p>Below is my <code>drawRect()</code> function that gets called each time a new set of data is ready. <code>N</code> holds the number of points and <code>points</code> is a <code>CGPoint*</code> vector with the coordinates to draw.</p>
  
  <pre><code>- (void)drawRect:(CGRect)rect {
  
      CGContextRef context = UIGraphicsGetCurrentContext();
  
      // set attributes
      CGContextSetStrokeColorWithColor(context, [UIColor lightGrayColor].CGColor);
      CGContextSetLineWidth(context, 1.f);
  
      // create path
      CGMutablePathRef path = CGPathCreateMutable();
      CGPathAddLines(path, NULL, points, N+1);
  
      // stroke path
      CGContextAddPath(context, path);
      CGContextStrokePath(context);
  
      // clean up
      CGPathRelease(path); 
  }
  </code></pre>
  
  <p>I tried rendering the path to an offline CGContext first before adding it to the current layer as suggested <a href="https://stackoverflow.com/questions/6963857/cglayer-and-anti-aliased-cgpaths">here</a>, but without any positive result. I also fiddled with an approach drawing to the CALayer directly but that too made no difference.</p>
  
  <p>Any suggestions how to improve performance for this task? Or is the rendering simply more work for the CPU that I realize? Would OpenGL make any sense/difference?</p>
  
  <p>Thanks /Andi</p>
  
  <p><strong>Update:</strong> I also tried using <code>UIBezierPath</code> instead of <code>CGPath</code>. This post <a href="https://stackoverflow.com/questions/6327817/why-is-uibezierpath-faster-than-core-graphics-path">here</a> gives a nice explanation why that didn't help. Tweaking <code>CGContextSetMiterLimit</code> et al. also didn't bring great relief. </p>
  
  <p><strong>Update #2:</strong> I eventually switched to OpenGL. It was a steep and frustrating learning curve, but the performance boost is just incredible. However, CoreGraphics' anti-aliasing algorithms do a nicer job than what can be achieved with 4x-multisampling in OpenGL.</p>