Why is this script taking more time in python3 than python2?

sayantankhan

I wrote this script to determine the first triangular number to have more than 500 factors (a project euler problem) and ran it on both python2.7 and python3.3. I expected them to take more or less the same time but python3 take more than twice the time that python2.7 does. What could be the reason behind this?

    def highest_power(integer,prime):
        i=0
        while integer%prime**i==0:
            i+=1
        return i-1

    def n_factors(integer):
        number_of_factors=1
        start=2
        while integer>1 and integer>=start:
            number_of_factors=number_of_factors*(highest_power(integer,start)+1)
            integer=integer/start**highest_power(integer,start)
            start+=1
        return number_of_factors

    def main(number_of_factors):
        number_of_factors_list=[1,1] # Initialized with number of factors for m=1 and 2
        m=3
        while number_of_factors_list[-1]*number_of_factors_list[-2]<number_of_factors:
            if m%2!=0:
               number_of_factors_list.append(n_factors(m))
            elif m%2==0:
                number_of_factors_list.append(n_factors(m/2))
            m+=1
       return (m-2)*(m-1)/2

    if __name__=='__main__':
        print(main(500))

Here are their timings

   $ time python2 script.py
   real 0m12.787s
   user 0m12.788s
   sys  0m0.000s

   $ time python3 script.py
   real 0m27.738s
   user 0m27.739s
   sys  0m0.000s

I'm running this on ubuntu 13.10 64 bit, with the precompiled binaries of python2.7 and python3.3.

chepner

You are using integer division in Python 2, and floating-point division in Python 3. See if adding

from __future__ import division

to the script increases the runtime under Python 2.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python3 Parse more than 30 videos at a time from youtube

From Dev

Python3 Parse more than 30 videos at a time from youtube

From Dev

Why the program run last much more time than 3 seconds?

From Dev

Why is my html table taking up more height than necessary?

From Dev

Why node.js process taking more memory than allocated

From Dev

Why WHILE is more efficient than FOR in this python function

From Dev

Flask: Execute time taking python script and direct the user to another page

From Dev

Run my script more than one time

From Dev

Why run guake by python2 but there are 'python3' in errors?

From Dev

Why does running my Python script start taking a screenshot?

From Dev

Any Reason why spring-hibernate is taking more time?

From Dev

Why is it possible to use more than 2 ^ 16 constants in a Python function?

From Dev

Why is it possible to use more than 2 ^ 16 constants in a Python function?

From Dev

why is there more than one python 2 command in ubuntu 12.04?

From Dev

Why did the Python developers make Python 3 have more parentheses than Python 2?

From Dev

Instagram client review taking more than 3 days

From Dev

Why does str(float) return more digits in Python 3 than Python 2?

From Dev

gprof not displaying time i e it is displaying 0.0% time even if it is taking more than 20 min?

From Dev

Taking multiple lists as arguments rather than returning them in a dict, is there a better way (python3)?

From Dev

How to parse same argument twice or more than once to python script?

From Dev

Allow more than one python threads to use resource at the same time

From Dev

Can importlib import more than one module at a time in Python 2.7?

From Dev

Python takes more time to print a calculation than to perform it

From Dev

Allow more than one python threads to use resource at the same time

From Dev

Can importlib import more than one module at a time in Python 2.7?

From Dev

Python: Convert string to datetime, calculate time difference, and select rows with time difference more than 3 days

From Dev

Passing a python object from a python2 script to python3 script

From Dev

Why the time taken by a thread is more than the total time taken by a process?

From Dev

python3 - why is size of string bigger than encode

Related Related

  1. 1

    Python3 Parse more than 30 videos at a time from youtube

  2. 2

    Python3 Parse more than 30 videos at a time from youtube

  3. 3

    Why the program run last much more time than 3 seconds?

  4. 4

    Why is my html table taking up more height than necessary?

  5. 5

    Why node.js process taking more memory than allocated

  6. 6

    Why WHILE is more efficient than FOR in this python function

  7. 7

    Flask: Execute time taking python script and direct the user to another page

  8. 8

    Run my script more than one time

  9. 9

    Why run guake by python2 but there are 'python3' in errors?

  10. 10

    Why does running my Python script start taking a screenshot?

  11. 11

    Any Reason why spring-hibernate is taking more time?

  12. 12

    Why is it possible to use more than 2 ^ 16 constants in a Python function?

  13. 13

    Why is it possible to use more than 2 ^ 16 constants in a Python function?

  14. 14

    why is there more than one python 2 command in ubuntu 12.04?

  15. 15

    Why did the Python developers make Python 3 have more parentheses than Python 2?

  16. 16

    Instagram client review taking more than 3 days

  17. 17

    Why does str(float) return more digits in Python 3 than Python 2?

  18. 18

    gprof not displaying time i e it is displaying 0.0% time even if it is taking more than 20 min?

  19. 19

    Taking multiple lists as arguments rather than returning them in a dict, is there a better way (python3)?

  20. 20

    How to parse same argument twice or more than once to python script?

  21. 21

    Allow more than one python threads to use resource at the same time

  22. 22

    Can importlib import more than one module at a time in Python 2.7?

  23. 23

    Python takes more time to print a calculation than to perform it

  24. 24

    Allow more than one python threads to use resource at the same time

  25. 25

    Can importlib import more than one module at a time in Python 2.7?

  26. 26

    Python: Convert string to datetime, calculate time difference, and select rows with time difference more than 3 days

  27. 27

    Passing a python object from a python2 script to python3 script

  28. 28

    Why the time taken by a thread is more than the total time taken by a process?

  29. 29

    python3 - why is size of string bigger than encode

HotTag

Archive