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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:56:44+00:00 2026-05-15T15:56:44+00:00

Consider the following code (from here , with the number of tests increased): from

  • 0

Consider the following code (from here, with the number of tests increased):

from timeit import Timer

def find_invpow(x,n):
    """Finds the integer component of the n'th root of x,
    an integer such that y ** n <= x < (y + 1) ** n.
    """
    high = 1
    while high ** n < x:
        high *= 2
    low = high/2
    while low < high:
        mid = (low + high) // 2
        if low < mid and mid**n < x:
            low = mid
        elif high > mid and mid**n > x:
            high = mid
        else:
            return mid
    return mid + 1

def find_invpowAlt(x,n):
    """Finds the integer component of the n'th root of x,
    an integer such that y ** n <= x < (y + 1) ** n.
    """
    low = 10 ** (len(str(x)) / n)
    high = low * 10
    while low < high:
        mid = (low + high) // 2
        if low < mid and mid**n < x:
            low = mid
        elif high > mid and mid**n > x:
            high = mid
        else:
            return mid
    return mid + 1

x = 237734537465873465
n = 5
tests = 1000000

print "Norm", Timer('find_invpow(x,n)', 'from __main__ import find_invpow, x,n').timeit(number=tests)
print "Alt", Timer('find_invpowAlt(x,n)', 'from __main__ import find_invpowAlt, x,n').timeit(number=tests)

Using Python 2.6 (Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2), the times reported are:

Norm 9.73663210869
Alt 9.53973197937

However, on the same machine using Python 3.1 (Python 3.1.2 (r312:79147, Apr 15 2010, 15:35:48) [GCC 4.4.3] on linux2), the times are:

Norm 28.4206559658
Alt 26.8007400036

Does anyone know why this code runs three times slower on Python 3.1?

  • 1 1 Answer
  • 2 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-15T15:56:44+00:00Added an answer on May 15, 2026 at 3:56 pm

    I got steadily decreasing times from 2.5, 2.6, 2.7 and 3.1 (Windows XP SP2) … with the “/” version. With the //, the 3.1 times were dramatically smaller than the 2.X times e.g. “Norm” dropped from 6.35 (py2.7) to 3.62 (py3.1).

    Note that in 2.x, there are ints (machine word, 32 or 64 bits) and longs (variable length). In 3.x, long has been renamed int, and int went away. My guess is that converting from long to float may cause the extra time with /.

    In any case, a much better “Alt” version would start off with this code:

    high = 1
    highpown = 1
    while highpown < x:
        high <<= 1
        highpown <<= n
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following code: from PIL import Image, ImageDraw, ImageFont def addText(img, lTxt): FONT_SIZE
Consider the following code from index view: <td> <%= 'foo' if bar == BAZ
Please consider the following example code (from the lm doc): ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14) trt
Let's consider the following scenario: a function which can generate code colors from white
Consider the following code (from a requirement that says that 3 is special for
Consider the following code: var Products_First = (from Entities.Product p in myContext.Product select p);
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Consider the following code: class MyCustomDescriptor: def __init__(self,foo): self._foo = foo def __call__(self,decorated_method): #
Consider the following code: ( Live demo here - Open in Internet Explorer 7

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.