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 8477435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:28:04+00:00 2026-06-10T18:28:04+00:00

Sometimes I have a lot of prints scattered around function to print debug output.

  • 0

Sometimes I have a lot of prints scattered around function to print debug output.
To switch this debug outputs I came up with this:

def f(debug=False): 
    print = __builtins__.print if debug else lambda *p: None

Or if I need to print something apart from debug message, I create dprint function for debug messages.

The problem is, when debug=False, this print statements slow down the code considerably, because lambda *p: None is still called, and function invocation are known to be slow.

So, my question is: Is there any better way to efficiently disable all these debug prints for them not to affect code performance?


All the answers are regarding my not using logging module. This is a good to notice, but this doesn’t answer the question how to avoid function invocations that slow down the code considerably – in my case 25 times (if it’s possible (for example by tinkering with function code object to through away all the lines with print statements or somehow else)). What these answers suggest is replacing print with logging.debug, which should be even slower. And this question is about getting rid of those function calls completely.

I tried using logging instead of lambda *p: None, and no surprise, code became even slower.


Maybe someone would like to see the code where those prints caused 25 slowdown: http://ideone.com/n5PGu

And I don’t have anything against logging module. I think it’s a good practice to always stick to robust solutions without some hacks. But I thinks there is nothing criminal if I used those hacks in 20-line one-time code snippet.


Not as a restriction, but as a suggestion, maybe it’s possible to delete some lines (e.g. starting with print) from function source code and recompile it? I laid out this approach in the answer below. Though I would like to see some comments on that solution, I welcome other approaches to solving this problem.

  • 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-06-10T18:28:05+00:00Added an answer on June 10, 2026 at 6:28 pm

    Ned Batchelder wrote in the comment:

    I suspect the slow down is in the calculation of the arguments to
    your debug function. You should be looking for ways to avoid those
    calculations. Preprocessing Python is just a distraction.

    And he is right as slowdown is actually caused by formatting string with format method which happens regardless if the resulting string will be logged or not.

    So, string formatting should be deferred and dismissed if no logging will occur. This may be achieved by refactoring dprint function or using log.debug in the following way:

    log.debug('formatted message: %s', interpolated_value)
    

    If message won’t be logged, it won’t be formatted, unlike print, where it’s always formatted regardless of if it’ll be logged or discarded.

    The solution on log.debug‘s postponed formatting gave Martijn Pieters here.

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

Sidebar

Related Questions

I am sure a lot of you have experienced this. Sometimes while debugging a
I feel silly asking this question... Sometimes I have a domain that I want
I have an directory, which has a lot of subdirectories. those subdirs sometimes even
I have a lot of client programs and one service. This Client programs communicate
I have this problem with a lot of arrays in my program, and I
At work I have to deal with a lot of (sometimes awful) JavaScript that
I have a lot of commands in one procedure and there is this command:
We have a lot of multi-statement TVFs and we sometimes want to convert these
Sometimes our forms have a lot of checkboxes in a table column. The user
I see on sites that they sometimes have a statistic showing how many views

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.