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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:46:59+00:00 2026-05-13T07:46:59+00:00

In pondering optimization of code, I was wondering which was more expensive in python:

  • 0

In pondering optimization of code, I was wondering which was more expensive in python:

if x:
    d = 1
else:
    d = 2

or

d = 2
if x:
    d = 1

Any thoughts? I like the reduced line count in the second but wondered if reassignment was more costly than the condition switching.

  • 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-13T07:46:59+00:00Added an answer on May 13, 2026 at 7:46 am

    Don’t ponder, don’t wonder, measure — with timeit at the shell command line (by far the best, simplest way to use it!). Python 2.5.4 on Mac OSX 10.5 on a laptop…:

    $ python -mtimeit -s'x=0' 'if x: d=1' 'else: d=2'
    10000000 loops, best of 3: 0.0748 usec per loop
    $ python -mtimeit -s'x=1' 'if x: d=1' 'else: d=2'
    10000000 loops, best of 3: 0.0685 usec per loop
    $ python -mtimeit -s'x=0' 'd=2' 'if x: d=1'
    10000000 loops, best of 3: 0.0734 usec per loop
    $ python -mtimeit -s'x=1' 'd=2' 'if x: d=1'
    10000000 loops, best of 3: 0.101 usec per loop
    

    so you see: the “just-if” form can save 1.4 nanoseconds when x is false, but costs 40.2 nanoseconds when x is true, compared with the “if/else” form; so, in a micro-optimization context, you should use the former only if x is 30 times more likely to be false than true, or thereabouts. Also:

    $ python -mtimeit -s'x=0' 'd=1 if x else 2'
    10000000 loops, best of 3: 0.0736 usec per loop
    $ python -mtimeit -s'x=1' 'd=1 if x else 2'
    10000000 loops, best of 3: 0.076 usec per loop
    

    …the ternary operator of the if/else has its own miniscule pluses and minuses.

    When the differences are as tiny as this, you should measure repeatedly, establish what the noise level is, and ensure you’re not taking differences “in the noise” as significant. For example, to compare statement vs expression if/else in the “x is true” case, repeat each a few times:

    $ python -mtimeit -s'x=1' 'd=1 if x else 2'
    10000000 loops, best of 3: 0.076 usec per loop
    $ python -mtimeit -s'x=1' 'd=1 if x else 2'
    10000000 loops, best of 3: 0.0749 usec per loop
    $ python -mtimeit -s'x=1' 'd=1 if x else 2'
    10000000 loops, best of 3: 0.0742 usec per loop
    $ python -mtimeit -s'x=1' 'd=1 if x else 2'
    10000000 loops, best of 3: 0.0749 usec per loop
    $ python -mtimeit -s'x=1' 'd=1 if x else 2'
    10000000 loops, best of 3: 0.0745 usec per loop
    

    now you can state that the expression forms takes (on this machine and versions of key software) 74.2 to 76.0 nanoseconds — the range is much more expressive than any single number would be. And similarly:

    $ python -mtimeit -s'x=1' 'if x: d=1' 'else: d=2'
    10000000 loops, best of 3: 0.0688 usec per loop
    $ python -mtimeit -s'x=1' 'if x: d=1' 'else: d=2'
    10000000 loops, best of 3: 0.0681 usec per loop
    $ python -mtimeit -s'x=1' 'if x: d=1' 'else: d=2'
    10000000 loops, best of 3: 0.0687 usec per loop
    $ python -mtimeit -s'x=1' 'if x: d=1' 'else: d=2'
    10000000 loops, best of 3: 0.0679 usec per loop
    $ python -mtimeit -s'x=1' 'if x: d=1' 'else: d=2'
    10000000 loops, best of 3: 0.0692 usec per loop
    

    now you can state confidently that the statement form takes (under identical conditions) 67.9 to 69.2 nanoseconds; so its advantage, for x true, wrt the expression form, is 4.8 to 8.1 nanoseconds (it’s quite fair to restrict this latter interval estimation to 6.3 to 6.8 nanoseconds, comparing min/min and max/max instead of min/max and max/min as the wider, more prudential estimate does).

    How much time and energy is worth devoting to such microscopic differences, once you’ve realized for any given care that they are microscopic, is, of course, a different issue.

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

Sidebar

Related Questions

I was wondering if there is a possible optimization where the compiler does not
I was wondering what -fno-omit-frame-pointer will do without optimization? CXXFLAGS = -Wall -ggdb3 -DDEBUG
I'm wondering if I can use a static variable for optimization: public function Bar()
I am pondering the alternatives on how to scroll an infinite, scale-like, control in
I'm wondering what is the fastest way that I can write some code. I
Good afternoon all, I have a class which looks like this: public class Grapheme
I've been starting to write many more unit tests for my code (something I
I recently came across the term Polymorphic Code , and was wondering if anyone
I have some code i'm revewing, which is used to convert some text into
I've been wondering this for some time. As the title say, which is faster,

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.