tags:

views:

758

answers:

4

when we want to release app on iPhone if I disable (comment NSLog();) it will get more advantage?

A: 

Not much of a performance change, but it won't reveal potentially internal info and it looks nicer for developers that happen to be checking the system log.

In short, you should disable it for release builds.

chpwn
thanks for your suggestion chpwn
RAGOpoR
A: 

Yes, you should disable it. Especially if you're trying to maximize the speed of your code. NSLogging things left and right pollutes the system log that other developers might be trying to dig through and it can have a big impact on speed-critical code (inside loops, etc..) I accidentally left some log messages in a recursive function once and got to release an update with a "30% speed increase!" a few weeks later... ;-)

Ben Gotow
"30% speed increase!" is really big... Of course, you should not put logging into small loops or recursive function. That doesn't meen that you can't do some smart logging.
gcamp
thanks for your suggestion Ben Gotow
RAGOpoR
+6  A: 

One way to do it is to go into your Build settings and under the Debug configuration add a value to "Preprocessor Macros" value like:

DEBUG_MODE=1

Make sure you only do this for the Debug configuration and not for Beta or Release versions. Then in a common header file you can do something like:

#ifdef DEBUG_MODE
#define DLog( s, ... ) NSLog( @"<%p %@:(%d)> %@", self, [[NSString stringWithUTF8String:__FILE__] lastPathComponent], __LINE__, [NSString stringWithFormat:(s), ##__VA_ARGS__] )
#else
#define DLog( s, ... ) 
#endif

Now instead of NSLog use DLog everywhere. When testing and debugging, you'll get debug messages. When you're ready to release a beta or final release, all those DLog lines automatically become empty and nothing gets emitted. This way there's no manual setting of variables or commenting of NSLogs required. Picking your build target takes care of it.

Ramin
thanks for your suggestion Ramin
RAGOpoR
A: 

NSLog is slow and should not be used for release builds. A simple macro like the one below will disable it along with any asserts you might have which should also be disabled. In the less common case where you do want NSLog in a release build, just call it directly. Don't forget to add "-DNDEBUG" to your "other c flags" build settings.

#ifdef NDEBUG
#define MYLog(f, ...) 
#else
#define MYLog(f, ...) NSLog(f, ## __VA_ARGS__)
#endif
papahabla
thanks for your suggestion papahabla
RAGOpoR