Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6053249
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:01:09+00:00 2026-05-23T08:01:09+00:00

Here i will add logger.isDebugEnabled() conditional statement for logger.debug(). But sometime there are many

  • 0

Here i will add logger.isDebugEnabled() conditional statement for logger.debug().
But sometime there are many logger.debug() in loop. For example:

Logger log = Logger.getLogger(Test.class);

for(int i = 0; i < 1000; i++) {
    ...
    log.debug("aaaaa");
    ...
    for(int j = 0; i < 100; j++) {
        ...
        log.debug("bbbb");
    }
}

If i add it directly, as follows:

for(int i = 0; i < 1000; i++) {
    ...
    if(log.isDebugEnabled()) {
        log.debug("aaaaa");
    }
    ...
    for(int j = 0; i < 100; j++) {
        ...
        if(log.isDebugEnabled()) {
            log.debug("bbbb");
         }
    }
}

So, in the loop, it will if() many times. How could i use less if(logger.isDebugEnabled())?

Does anyone have some idea ?
thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:01:09+00:00Added an answer on May 23, 2026 at 8:01 am

    What i care is that so may if() in loop whether will affect performance ?

    Consider this code:

        boolean logging = log.isDebugEnabled();
    
        for (int i = 0; i < 1000; i++) {
            // stuff
            if (logging) { 
                log.debug("Hi Mum!");
            }
            // more stuff
        }
    

    The cost of the if test could be as small as 2 instructions, depending how the JIT compiler’s register allocation pans out. This is most likely insignificant, unless you are doing ridiculous amounts of logging.

    However, if the performance hit of those 2 to 4 instructions really matters, then you could consider:

    • removing the call to log entirely,

    • making it conditional on a compile time constant (so that the optimizer can prune the code), or

    • hoisting the test out of the loop; e.g. restructuring the code as follows:

      boolean logging = log.isDebugEnabled();
      
      if (logging) {
          for (int i = 0; i < 1000; i++) {
              // stuff
              log.debug("Hi Mum!");
              // more stuff
          }
      } else {
          for (int i = 0; i < 1000; i++) {
              // stuff
              // more stuff
          }
      } 
      

    However, IMO, the cure is worse than the disease.


    @Vineet’s point is important too. In practice, the expensive part of something like this:

    log.debug("Today is " + date);
    

    is that the String concatenation expression is evaluated irrespective of the actual logging level. There are other ways to avoid this overhead apart from an if test … though they will be more expensive than an if test on a cached flag.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example, here on stack overflow the URL https://stackoverflow.com/questions/tagged/javascript+php will give you all questions
Here is the scenario: I'm writing an app that will watch for any changes
any bit of info will be helpful here. or does anybody else huge do
Here's my situation: I'm trying to create a SSL certificate that will be installed
Here's the idea, I'd like to make a service? that will look for a
Alright so here is my issue. I'm working a game engine that will eventually
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.