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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:29:31+00:00 2026-06-15T17:29:31+00:00

Let’s say I want to divide two variables, in Python 2.* (mainly 6 and

  • 0

Let’s say I want to divide two variables, in Python 2.* (mainly 6 and 7), that are considered integers. For instance:

a, b = 3, 2
print a/b
# Prints "1"

Now, there are at least two (non-redundant) ways I know of to cause this division to be normal, floating point division (without running a from __future__ import division). They are:

print a*1.0/b      # Of course you could multiply b by 1.0 also 

and

print float(a)/b   # Here you could also have cast b as a float

Does one of these methods have an advantage (in speed) over the other? Does one have more overhead than the other?

  • 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-15T17:29:32+00:00Added an answer on June 15, 2026 at 5:29 pm
    >>> timeit.timeit(stmt="a*1.0/b",setup="a,b=3,2",number=100)
    4.669614510532938e-05
    >>> timeit.timeit(stmt="float(a)/b",setup="a,b=3,2",number=100)
    7.18402232422477e-05
    

    From the above, you can tell that simply using a*1.0/b is much faster then using float(a). This is because calling functions in Python are very costly. That being said though, you could do something like:

    a,b=float(3),2
    print a/b
    

    and you would have the benchmark of:

    >>> timeit.timeit(stmt="a/b",setup="a,b=float(3),2",number=100)
    2.5144078108496615e-05
    

    This is because you only call float() once, and that is on assignment of a. This in turn doesn’t require the 1.0*a to be factored in, giving a much faster result.

    Breaking this down further using the dis module, you can see the actual calls for this in a loop:

    float during division

    def floatmethod():
        a,b=3,2
        while True:
            print float(a)/b
    

    float during division dis results

    dis.dis(floatmethod)
      2           0 LOAD_CONST               3 ((3, 2))
                  3 UNPACK_SEQUENCE          2
                  6 STORE_FAST               0 (a)
                  9 STORE_FAST               1 (b)
    
      3          12 SETUP_LOOP              25 (to 40)
            >>   15 LOAD_GLOBAL              0 (True)
                 18 POP_JUMP_IF_FALSE       39
    
      4          21 LOAD_GLOBAL              1 (float)
                 24 LOAD_FAST                0 (a)
                 27 CALL_FUNCTION            1
                 30 LOAD_FAST                1 (b)
                 33 BINARY_DIVIDE       
                 34 PRINT_ITEM          
                 35 PRINT_NEWLINE       
                 36 JUMP_ABSOLUTE           15
            >>   39 POP_BLOCK           
            >>   40 LOAD_CONST               0 (None)
                 43 RETURN_VALUE        
    

    Reason for speed decrease

    The reason that this method is much slower is because it must first LOAD_GLOBAL: float, then grab the value of a (LOAD_FAST: a) then it calls float(a) (CALL_FUNCTION). It then finally executes the division (BINARY_DIVIDE), all of which done over and over during the loop.

    float on assignment

    def initfloatmethod():
        a,b=float(3),2
        while True:
            print a/b
    

    float on assignment dis results

    dis.dis(initfloatmethod)
      2           0 LOAD_GLOBAL              0 (float)
                  3 LOAD_CONST               1 (3)
                  6 CALL_FUNCTION            1
                  9 LOAD_CONST               2 (2)
                 12 ROT_TWO             
                 13 STORE_FAST               0 (a)
                 16 STORE_FAST               1 (b)
    
      3          19 SETUP_LOOP              19 (to 41)
            >>   22 LOAD_GLOBAL              1 (True)
                 25 POP_JUMP_IF_FALSE       40
    
      4          28 LOAD_FAST                0 (a)
                 31 LOAD_FAST                1 (b)
                 34 BINARY_DIVIDE       
                 35 PRINT_ITEM          
                 36 PRINT_NEWLINE       
                 37 JUMP_ABSOLUTE           22
            >>   40 POP_BLOCK           
            >>   41 LOAD_CONST               0 (None)
                 44 RETURN_VALUE      
    

    Reason for speed increase

    You can see that on the line in which the division is performed, it no longer has to call the float function, allowing immediate execution of the division. It simply calls LOAD_GLOBAL: float and calls CALL_FUNCTION once, which is on assignment, rather then in the loop. This means it can skip straight to the BINARY_DIVIDE call.

    Stats used for this benchmark:

    Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have a string: abcbcdcde I want to identify all substrings that
Let's say that you want to create a dead simple BlogEditor and, one of
Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let me explain best with an example. Say you have node class that can
Let's say that I have a SQLite database that I create in a separate
Let's say I have thousands of users and I want to make the passwords
Let's say I have multiple requirements for a password. The first is that the
Let's say that I have a date in R and it's formatted as follows.
Let's say I have a drive such as C:\ , and I want to
Let's say I'm writing a PHP (>= 5.0) class that's meant to be a

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.