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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:40:09+00:00 2026-05-14T01:40:09+00:00

In another question , the accepted answer suggested replacing a (very cheap) if statement

  • 0

In another question, the accepted answer suggested replacing a (very cheap) if statement in Python code with a try/except block to improve performance.

Coding style issues aside, and assuming that the exception is never triggered, how much difference does it make (performance-wise) to have an exception handler, versus not having one, versus having a compare-to-zero if-statement?

  • 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-14T01:40:10+00:00Added an answer on May 14, 2026 at 1:40 am

    Why don’t you measure it using the timeit module? That way you can see whether it’s relevant to your application.

    OK, so I’ve just tried the following (using Python 3.11.1 on Windows 11):

    import timeit
    
    statements=["""\
    try:
        b = 10/a
    except ZeroDivisionError:
        pass""",
    """\
    if a:
        b = 10/a""",
    "b = 10/a"]
    
    for a in (1,0):
        for s in statements:
            t = timeit.Timer(stmt=s, setup='a={}'.format(a))
            print("a = {}\n{}".format(a,s))
            print("%.2f usec/pass\n" % (1000000 * t.timeit(number=100000)/100000))
    

    Result:

    a = 1
    try:
        b = 10/a
    except ZeroDivisionError:
        pass
    0.06 usec/pass
    
    a = 1
    if a:
        b = 10/a
    0.05 usec/pass
    
    a = 1
    b = 10/a
    0.03 usec/pass
    
    a = 0
    try:
        b = 10/a
    except ZeroDivisionError:
        pass
    0.27 usec/pass
    
    a = 0
    if a:
        b = 10/a
    0.02 usec/pass
    
    a = 0
    b = 10/a
    Traceback (most recent call last):
      File "<stdin>", line 5, in <module>
      File "C:\Python311\Lib\timeit.py", line 178, in timeit
        timing = self.inner(it, self.timer)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^
      File "<timeit-src>", line 6, in inner
    ZeroDivisionError: division by zero
    

    As you can see, there is not much of a difference between using a try/except clause vs. an explicit if statement, unless the exception gets triggered. (And of course, not having any control structure is fastest, though not by much, and it will crash the program if anything goes wrong).

    Compare this to the results obtained in 2010:

    a = 1
    try:
        b = 10/a
    except ZeroDivisionError:
        pass
    0.25 usec/pass
    
    a = 1
    if a:
        b = 10/a
    0.29 usec/pass
    
    a = 1
    b = 10/a
    0.22 usec/pass
    
    a = 0
    try:
        b = 10/a
    except ZeroDivisionError:
        pass
    0.57 usec/pass
    
    a = 0
    if a:
        b = 10/a
    0.04 usec/pass
    
    a = 0
    b = 10/a
    ZeroDivisionError: int division or modulo by zero
    

    I appears that the PC I’m using now is about twice as fast as the one I had back then. The cost of handling an Exception appears identical, and the "normal" operations (arithmetic) have been improved even more than the handling of control structures, but the point from all those years ago still stands:

    It’s all within the same order of magnitude and unlikely to matter either way. Only if the condition is actually met (often), then the if version is significantly faster.

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

Sidebar

Ask A Question

Stats

  • Questions 530k
  • Answers 530k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer i think RIPE has usage limits - you may be… May 16, 2026 at 11:39 pm
  • Editorial Team
    Editorial Team added an answer echo date('Y', strtotime('+1 year')); May 16, 2026 at 11:39 pm
  • Editorial Team
    Editorial Team added an answer The only real reason I can think of is when… May 16, 2026 at 11:39 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Can you give me a good start for this? Thanks! Edit: The accepted answer
This stackoverflow question has an interesting discussion on how to avoid giving enums and
I have written a little python application and here you can see how Task
I'm creating a program in C# using System.Speech.Recognition that recognizes voice commands and translates
Update: A sample project reproducing this bug can be found here at Microsoft Connect
-- Original Post -- I am trying to manage (start/stop) a windows service on
I need to get the spring application context from a non bean object. In
I'm having a problem with my compiler telling me there is an 'undefined reference
Is it possible to make a link such as: <a href=anton://useful_info_for_anton_app>click me!</a> cause my
I am in the process of developing a simple JFrame based GUI to which

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.