2

I am trying to optimise my app to the as best as it can get, can you please suggest which method is best, and recommended.

@implementation Methode1   
+(BOOL)Isdone{
      BOOL result = [[NSUserDefaults standardUserDefaults] boolForKey:@"DEVICE_TYPE"];
      if(!result){
        [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"DEVICE_TYPE"];

      }
     return result;
}
@end

@implementation Methode2 
NSString * const deviceTypeKey @"DEVICE_TYPE";
+(BOOL)Isdone{
  BOOL result = [[NSUserDefaults standardUserDefaults] boolForKey:deviceTypeKey];
  if(!result){
     [[NSUserDefaults standardUserDefaults]setBool:YES forKey:deviceTypeKey];
   }
  return result;
}
@end

@implementation Methode3 
#define deviceTypeKey @"DEVICE_TYPE"
+(BOOL)Isdone{
  BOOL result = [[NSUserDefaults standardUserDefaults] boolForKey:deviceTypeKey];
  if(!result){
     [[NSUserDefaults standardUserDefaults]setBool:YES forKey:deviceTypeKey];
   }
  return result;
}
@end

in the three above mentioned method which one is most memory efficient and why?

  • 2
    If you have optimized everything else already and this is your biggest memory problem... Well, congratulations for writing such code. You may stop optimizing now, because it's getting ridiculous. If that's not your biggest memory hog, then stop optimizing prematurely and consult the profiler asap. – JustSid Aug 30 '13 at 07:30

1 Answers1

3

None of them will make the slightest significant difference in terms of memory. Constant string literals are optimised by the compiler for you.

What's more important is the risk of programmer errors and maintainability - so using a constant or a define for your defaults keys is the way to go. I'm a constants fan myself.

Also - see @JustSid's comment. You need to use instruments to look for genuine problems, don't pick random bits of code and agonise over them. Write for mainainabilty and readability first, then find actual problems by profiling.

jrturton
  • 118,105
  • 32
  • 252
  • 268
  • My app is just fine in profiler, static analyser etc. every thing is well in control. I got a commend from my collie that the use of method one(Methode1) is more memory consuming :) as memory for @"DEVICE_TYPE" is given in memory twice, to clarify this i didn't find any relevant documents. other methods are also implemented in my app, so just to clarify my doubt, which is the best and opted method i asked this question. "THANKS FOR YOUR REPLYS". BUT STIL any one explain me which wil use least memory foot print.and why, just out of curiosity. and in first case will compiler do any optimisation? – Edwin Wilson Aug 30 '13 at 08:43
  • See http://stackoverflow.com/questions/8032375/difference-between-nsstring-literals for more information. – jrturton Aug 30 '13 at 15:57