2

I need to graph a large amount of data points using straight lines and an affine transformation (to scale the graphic so it can fit the view).

Currently, I am using NSBezierPath, but I believe it is quite inefficient (because points are copied to the bezier path prior to being graphed). I got improvements by cutting my data into 50-points chunks (NSBezier path is much faster).

But I understand there may be a better way using core graphic or CG.... function calls ?

My app is written in swift.

John
  • 77
  • 7

1 Answers1

1

First of all, if you can: profile your app and try to determine where your performance bottleneck lies.

My experience is that Core Graphics is not really quick at anything.

If you want to rasterize straight lines fast, you might want to research OpenVG implementations (there appears to be several open source implementations for iOS) or even use OpenGL directly. Of course, this assumes rendering lots and lots of lines fast is a differentiator for your app making it worth the effort.

Community
  • 1
  • 1
Krumelur
  • 31,081
  • 7
  • 77
  • 119
  • Thank you for your reply. Yes my app depends on it. Currently, I use `CGContextMoveToPoint`, `CGContextAddLineToPoint` and `CGContextStrokePath` but I have seen that putting 1000 points in the path is much much slower to draw than putting only 100 points. This meant it is actually drawn like a bezier path, despite the fact it contains only straight lines. How can I tell the system to draw only straight lines ? – John Aug 24 '15 at 21:34
  • How did you come to this conclusion? I always thought UIBezierPath was just an ObjC wrapper around a CGPath, but I would double check the docs before relying on it – Krumelur Aug 24 '15 at 21:37
  • According to Apple, it is indeed a wrapper: https://developer.apple.com/library/ios/documentation/2DDrawing/Conceptual/DrawingPrintingiOS/BezierPaths/BezierPaths.html – Krumelur Aug 24 '15 at 21:39
  • Back to my answer. CoreGraphics is not really the best performing way of drawing complex dynamic vector graphics with a high frame rate. I recommend that you at least evaluate how the other methods suit your problem, especially since the feature is so important to your app – Krumelur Aug 24 '15 at 21:41
  • 1
    How did you come to this conclusion? @Krumelur : well, I did the test. Graphing 100000 points in in 2000 CGContextStrokePath rather than in one is three orders of magnitude faster. Same for NSBezierPath. This is why I always cut the data in small sets before graphing. From point 1 to 50, then from 50 to 99, etc... – John Aug 26 '15 at 07:52