Timer Problem

Discussion in 'iOS Programming' started by xArtx, Oct 6, 2012.

  1. xArtx macrumors 6502a

    Joined:
    Mar 30, 2012
    #1
    Hi Guys,
    I have a timer set up to call a screen refresh so the drawrect function is called.

    Lately, I've ported a pi calculation routine I had working on another platform.
    The routine calcs 50000 digits of pi, and I want it to time that calculation.

    It appears the timer isn't called while the drawrect function is busy calculating pi.

    Any suggestions on how to fix this? or even if it should be a problem?
    Cheers, Art.
     
  2. itickings macrumors 6502a

    itickings

    Joined:
    Apr 14, 2007
    #2
    Based on what your design sounds like - Yes, it should be a problem.

    A couple of things worth thinking about: Don't do heavy lifting in the UI thread and don't rely on screen refreshes for performing calculations. As for timing the calculation, you could try to just get a timestamp before and after your routine runs, and comparing those.

    Try some alternative approaches and get back to us with what you've tried and how you've reasoned.
     
  3. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #3
    I wasn't aware NStime can give high resolution timings...
     
  4. itickings macrumors 6502a

    itickings

    Joined:
    Apr 14, 2007
    #4
    I wasn't aware you specified any resolution requirements... ;)

    What timing resolution do you need? How long is an average run? Is there variation in running time between runs? How do you account for other processes etc. competing for resources? What do you intend to use the results for?

    OK, back to topic. Have you double-checked that the calculation isn't performed directly by the UI/Timer?
     
  5. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #5
    Several things.

    First, triggering drawRect every screen refresh is a very bad way to do animation on iOS (or Mac OS for that matter). That means that for every screen refresh, the CPU has to render the entire frame, then copy it over to graphics memory, where the graphics hardware then has to render it.

    Whenever possible you should use graphics calls that do the "heavy lifting" with the GPU.

    What kind of drawing are you doing that requires drawRect? You should think through it and see if there is a way to refactor it to do the same thing using graphics calls. This can result in a 10x or greater speedup.

    Next, NSTimer is a lousy way to trigger animation. Its resolution is around 50 MS at best. Plus, NSTimer uses cooperative multi-tasking. The system only checks for timers going off in the main event loop on the main thread. If your code is busy doing something time-consuming when the timer goes off, it can be late, or even miss an entire timer fired event.

    If you want to do frame-based animation, you should look into CADisplayLink. To quote the docs:

    NSDate is a very accurate way to time long tasks. It is tied to the hardware clock, and gives sub-millisecond accuracy. You can use code like this:

    Code:
    NSTimeInterval start = [NSDate timeIntervalSinceReferenceDate];
    //do time-consuming stuff like calculating pi to 5000 places.
    NSTimeInterval end =  [NSDate timeIntervalSinceReferenceDate];
    NSTimeInterval elapsed = end - start;
    NSLog(@"The calculation took %.5f seconds", elapsed);
     
  6. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #6
    Hi again guys, & thx for the replies.

    First of all, the program is no more than a fun exercise,
    I had it working on the Sony PSP for a fancy demo:
    http://www.youtube.com/watch?v=f4dMHBIjS3I
    The iPhone version looks and sounds similar.

    Since doing it, timing the calculation routine was an afterthought.
    I thought it would be interesting to compare the time taken by my
    iphone 4S, with a plain iPhone 4, and an iPad 3 (all of which I have here).

    Tell me about it... this was a problem in doing my GPS app.
    THe screen has to be redrawn for every frame.
    I never figured out how to draw offscreen to a bitmap and display that bitmap either like I did on the last platform.

    PS that timer code worked, the calculation takes 17.9 seconds for 10,000 decimal places of pi.
     
  7. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #7
    New iPad takes 14.4 seconds,
    iphone 4S takes 17.6 seconds,
    iphone 4 takes 17.5 seconds.

    All for 10,000 digits.
    The iPhone 4 time fluctuates the most, but reliably quicker than the 4S.
     
  8. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #8
    Could you refactor your algorithm to run in parallel? That would give the 4S, iPhone 5, and new iPad a marked advantage.

    Are you using the NSDate timeIntervalSinceReferenceDate timing method I recommended in my previous post?
     
  9. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #9
    Yes, from the beginning of the first call to the end of the last, before anything is printed to screen.

    Apparently not. I'd like to, but no idea how. Should read an ios book one day.

    It would be more important for me to get a bitmap working,
    but never seen a full project example both writing to a bitmap and
    then displaying it to screen. Real working samples seem to be lacking where ios is concerned.
     
  10. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #10
    There are tons of working samples. If you read the Xcode docs often at teh bottom of a method or class there are links to download full-functioning sample apps that illustrate the thing you are reading about.

    Github also has lots of sample projects, and there are tons of blogs where people post fully functioning sample projects. There are also lots and lots of iOS books that either include a CD with code, or link to an online source where you can download the sample code from the book.
     
  11. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #11
    There are 2 parts to that question. The first part is, is the algorithm suitable for running in parallel? If no, stop. If yes, you get to the second part of the question.

    The second part of the question is how do you implement your parallel algorithm in iOS.

    The first part of the question is not iOS specific. The second part (obviously) is.
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Why is this important to you? Based on the YouTube video, you have a fairly simple label updating on an interval. Is there something else you eventually hope to work towards?
     
  13. xArtx thread starter macrumors 6502a

    Joined:
    Mar 30, 2012
    #13
    This project isn't important.
    Any improvement I can make would possibly be implemented in my personal GPS program though which would be nice.
     

Share This Page