Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 821953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:43:39+00:00 2026-05-15T02:43:39+00:00

in a simple drawing application I have a model which has a NSMutableArray curvedPaths

  • 0

in a simple drawing application I have a model which has a NSMutableArray curvedPaths holding all the lines the user has drawn. A line itself is also a NSMutableArray, containing the point objects. As I draw curved NSBezier paths, my point array has the following structure: linePoint, controlPoint, controlPoint, linePoint, controlPoint, controlPoint, etc… I thought having one array holding all the points plus control points would be more efficient than dealing with 2 or 3 different arrays.

Obviously my view draws the paths it gets from the model, which leads to the actual question: Is there a way to optimize the following code (inside the view’s drawRect method) in terms of speed?

int lineCount = [[model curvedPaths] count];

// Go through paths
for (int i=0; i < lineCount; i++)
{
    // Get the Color
    NSColor *theColor = [model getColorOfPath:[[model curvedPaths] objectAtIndex:i]];

    // Get the points
    NSArray *thePoints = [model getPointsOfPath:[[model curvedPaths] objectAtIndex:i]];

    // Create a new path for performance reasons
    NSBezierPath *path = [[NSBezierPath alloc] init];

    // Set the color
    [theColor set];

    // Move to first point without drawing
    [path moveToPoint:[[thePoints objectAtIndex:0] myNSPoint]];

    int pointCount = [thePoints count] - 3;

    // Go through points
    for (int j=0; j < pointCount; j+=3)
    {
        [path curveToPoint:[[thePoints objectAtIndex:j+3] myNSPoint] 
             controlPoint1:[[thePoints objectAtIndex:j+1] myNSPoint]
             controlPoint2:[[thePoints objectAtIndex:j+2] myNSPoint]];
    }

    // Draw the path
    [path stroke];

    // Bye stuff
    [path release];
    [theColor release];
}

Thanks,
xonic

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T02:43:39+00:00Added an answer on May 15, 2026 at 2:43 am

    Hey xon1c, the code looks good. In general it is impossible to optimize without measuring performance in specific cases.

    For example, lets say the code above is only ever called once. It draws a picture in a view and it never needs redrawing. Say the code above takes 50 milliseconds to run. You could rewrite it in openGL and do every optimisation under the sun and get that time down to 20 milliseconds and realistically the 30 milliseconds that you have saved makes no difference to anyone and you pretty much just wasted your time and added a load of code-bloat that is going to be more difficult to maintain.

    However, if the code above is called 50 times a second and most of those times it is drawing the same thing then you could meaningfully optimise it.

    When it comes to drawing the best way to optimise is to is to eliminate unnecessary drawing.

    Each time you draw you recreate the NSBezierPaths – are they always different? You may want to maintain the list of NSBezier paths to draw, keep that in sync with your model, and keep drawrect solely for drawing the paths.

    Are you drawing to areas of your view which don’t need redrawing? The argument to drawrect is the area of the view that needs redrawing – you could test against that (or getRectsBeingDrawn:count:), but it may be in your case that you know that the entire view needs to be redrawn.

    If the paths themselves don’t change often, but the view needs redrawing often – eg when the shapes of the paths aren’t changing but their positions are animating and they overlap in different ways, you could draw the paths to images (textures) and then inside drawrect you would draw the texture to the view instead of drawing the path to the view. This can be faster because the texture is only created once and uploaded to video memory which is faster to draw to the screen. You should look at Core Animation if this is what you need todo.

    If you find that drawing the paths is too slow you could look at CGPath

    So, on the whole, it really does depend on what you are doing. The best advice is, as ever, not to get sucked into premature optimisation. If your app isn’t actually too slow for your users, your code is just fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 395k
  • Answers 395k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer for each(item in colunas) { var itemok:String = item.dataField; Alert.show(''+datagridlist.selectedItem[itemok]);… May 15, 2026 at 2:44 am
  • Editorial Team
    Editorial Team added an answer The Nexus One IS the unlocked version of the Nexus… May 15, 2026 at 2:44 am
  • Editorial Team
    Editorial Team added an answer As someone noted, my code does work. I just tested… May 15, 2026 at 2:44 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.