views:

207

answers:

2

I am writing a small program to display current time on iPhone (learning :D). I came across this confusion.

Is calling currentSystemTime ( eg: stringFromDate: ) on every second, parse it and print the time on screen is good?

Would it be more effective to call the above routine once and manually update the parsed second every tick of your timer. (Say like ++seconds; write some if loops to adjust minutes and hour).

Will the second approach result in out-of-sync of with the actual time; if the processor load increases or so?

Considering all this which will be the best approach.

A: 

I would drop the seconds totally and just print the rest of the time then you only have to parse it once a minute.

That's if you want a clock rather than a stopwatch. Seriously, I can't remember the last time I looked at a clock without seconds and thought "Gosh, I don't know if it's 12:51:00 or 12:51:59. How will I make my next appointment?").

If you want to ensure you're relatively accurate in updating the minute, follow these steps:

  • Get the full time (HHMMSS).
  • Display down to minute resolution (HH:MM).
  • Subtract SS from 61 and sleep for that many seconds.
  • Go back to that first step.
paxdiablo
+1  A: 

I doubt that the overhead of querying the system time will be noticeable in comparison to the CPU cycles used to update the display. Set up an NSTimer to fire however often that you want to update the clock display, and update your display that way. Don't worry about optimizing it until you get the app working.

Mark Bessey