Is there any need to use NSLog?

As a new programmer, I discovered the magic of NSlog and use it all through my code. This was very useful (along with NSZombieEnabled ) when debugging.

I can see a certain performance hit on the simulator as it prints it all. I do not think that I see such a hit on devices, but I'm not sure.

So is it worth something to leave all NSLogs ? Does he use more memory on the device? Or does the compiler simply ignore them, like compilation comments for a device?

EDIT:

Here, what I have implemented is on a proposal from rano.

In my App_Prefix.pch file, I added:

 // DLog is almost a drop-in replacement for NSLog // DLog(); // DLog(@"here"); // DLog(@"value: %d", x); // Unfortunately this doesn't work DLog(aStringVariable); you have to do this instead DLog(@"%@", aStringVariable); #ifdef DEBUG # define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); #else # define DLog(...) #endif // ALog always displays output regardless of the DEBUG setting #define ALog(fmt, ...) NSLog((@"%s [Line %d] " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__); 

And then in my Project Info inspector for the Debug configuration under the GCC 4.2 - Preprocessing, heading GCC 4.2 - Preprocessing, I added the Debug value to the top entry called Preprocessor Macros.

Works like a spell - DLog displays when I build a version of Debug and ALog always issues.

+6
ios memory iphone nslog
source share
3 answers

NSLog sure more pricy than simple printf , but a bit more integrated into objective-C and Cocoa frameworks.

By the way, when you are seriously programming, you will notice that this is unacceptable for most of your needs. Take a look at this article and its links to the idea of โ€‹โ€‹how to replace it in a reasonable way.

That way, you can also make the compiler ignore it when it is no more useful (for example, when you release your piece of code). In general, you could #ifdef call the log function when calculating intense code loops / sequences.

+7
source share

NSLog not implemented as a macro, so it will definitely be worth it. Quantifying and deciding whether this is important is more difficult. The only sure way to do this is to measure your own application.

0
source share

The answers above are out of date. You just have to use CocoaLumberjack , significantly superior to NSLog and providing much more functionality than the answers above.

0
source share

All Articles