[slf4j-user] is*Enabled vs explicit method call performance measure

Chris Pratt thechrispratt at gmail.com
Fri Aug 19 08:18:20 CEST 2011


It would partially depend on your logging level, I assume you had it set
below debug so that both would produce the same results?  But I do think
something must be wonky with your test, I'd expect at most a 2x performance
hit from making 2 calls to the LOG object vs 1 (even though I suspect it
would be closer to 1:1 since internally LOG.debug has to call
LOG.isDebugEnabled or its equivalent).

With that being said, the time you really want to use isDebugEnabled is when
you have more than two parameters, or when one of the parameters requires
dereferencing.  In other words you don't need it for your example of:

LOG.debug("Some constant string: {} {}",obj1,obj2);

But you would need it in any of the following cases:

if(LOG.isDebugEnabled()) {
  LOG.debug("Lots of Arguments {}, {}, {}, {}",new Object[] {obj1, obj2,
obj3, obj4});  // Since the additional object creation is not necessary when
the logging level is below debug

  LOG.debug("Dereferenced Arguments {},
{}",obj1.getLastName(),obj1,getFirstName());  // Since unnecessary method
calls are performed when the log statement is not written

  LOG.debug("Some young noob " + obj1.getExplitive() + "'s up your " +
obj1.getPraise() + " code"); // Since building strings from concatenation
when is expensive
}

  (*Chris*)


On Thu, Aug 18, 2011 at 10:47 PM, coldserenity <rmuntyan at softserveinc.com>wrote:

> Hi,
>
>  The question is not specific to Slf4j, but it is an important part of
> slf4j usage.
>  Recently in my team we've been arguing which of the 2 methods would work
> faster
>            LOG.debug("Some constant string ", obj1, obj2); // method
> invocation passing parameters
>   OR
>            if (LOG.isDebugEnabled()) { // simple method invocation with if
> check
>                LOG.debug("Some constant string ", obj1, obj2);
>            }
>
>  So I've created a synthetic test (attached in archive
>
> http://slf4j.42922.n3.nabble.com/file/n3267209/isDebugEnabledPerformanceTest-src.zip
> isDebugEnabledPerformanceTest-src.zip ) which produces following strange
> results
>  Test console output for 50M iterations
>        Time to go with direct call: 2ms
>        Time to go with if statement: 31ms
>  For 500M iterations
>        Time to go with direct call: 3ms
>        Time to go with if statement: 278ms
>
>  The question is whether there's anything I did wrong in my synthetic test?
> Or the results are expected?
>
>  Thanks!
> Roman
>
> --
> View this message in context:
> http://slf4j.42922.n3.nabble.com/is-Enabled-vs-explicit-method-call-performance-measure-tp3267209p3267209.html
> Sent from the slf4j - user mailing list archive at Nabble.com.
> _______________________________________________
> slf4j-user mailing list
> slf4j-user at qos.ch
> http://qos.ch/mailman/listinfo/slf4j-user
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://qos.ch/pipermail/slf4j-user/attachments/20110818/04d6f661/attachment.html>


More information about the slf4j-user mailing list