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

Aayush Mahajan

I wrote a script in python, and it surprised me. Basically, it takes five 20 digit numbers, multiplies them and then raises them to the power of 3000. The timeit module is used to find the time required to compute the calculation. Well, when I run this script, it says it took 3*10^-7 seconds to compute it. It then generates a file, output.txt, but the script doesn't end until about 15 seconds later.

import timeit
outputFile = open("output.txt", "w")
start = timeit.default_timer()
x = (87459837581209463928*23745987364728194857*27385647593847564738*10293769154925693856*12345678901234567891)**3000
stop = timeit.default_timer()
time = stop-start
print "Time taken for the calculation was {} seconds".format(time)
outputFile.writelines(str(x))
outputFile.close()
y = raw_input("Press enter to exit.")

Does this mean that it actually takes a longer time to print a 280kb file than to perform the calculation?(I find it unlikely.)

If not so, then does python execute the calculation when the variable x is called upon? Will it execute the calculation every time the variable is calculated, or will it store the actual value in the variable?

I have just written another script, which confirms that it takes python 0.03 seconds to write the result to a .txt file. So, why does python execute the calculations later?

Tim Peters

It's not the calculation that's the problem, nor is it writing to file: the vast bulk of the time is consumed by converting the result from its internal binary representation to a base-10 representation. That takes time quadratic in the number of bits, and you have a lot of bits here.

If you replace your output line with:

outputFile.writelines(hex(x))

you'll see that it runs very much faster. Converting to a hex representation only takes time linear in the number of bits.

If you really need to output giant integers in base 10 representation, look into using the decimal module instead. That does computations internally in a representation related to base 10, and then conversion to a decimal string takes time linear in the number of decimal digits. You'll need to set the decimal context's precision to a "big enough" value in advance, though, to avoid losing lower-order digits to rounding.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python multiprocessing takes more time

From Dev

Python multiprocessing takes more time

From Dev

Code takes much more time to finish with more than 1 thread

From Dev

Curl from php takes more time than curl via putty

From Dev

logback: AsyncAppender takes more time than Synchronous FileAppender

From Dev

Why using static logger takes more time than not using it

From Dev

Background thread takes more time than UI thread

From Dev

Kill process if it takes more than xGiB of RAM in a given amount of time

From Dev

Python Generator that yields more results takes more time to create

From Dev

Python takes six times more memory than it should

From Dev

Calculation of a time interval a time list until the next time mark is more than x minutes away

From Dev

How to print more than one div at the same time in same pdf?

From Dev

can i perform operator overloading of more than 2 objects in python?

From Dev

Perform "bigger than" on more than two digits

From Dev

Populating table for more than 20k rows it takes lot of time and page becomes unresponsive when it done

From Dev

Improving query run time (Takes more than 20seconds to load a page)

From Dev

print comma separated if more than two values in the list python

From Dev

print more than one output for a particular condition in python

From Dev

Java-Calculator more than one calculation

From Dev

Calculation for more than one column in data frame

From Dev

JBrowserDriver takes more than a minute to initialize

From Dev

MySQL statement takes more than minute to execute

From Dev

JBrowserDriver takes more than a minute to initialize

From Dev

Kubuntu takes more than 2 minutes to boot!

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

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

Why does the camera calibration in opencv-python takes more than 30 minutes?

Related Related

  1. 1

    Python multiprocessing takes more time

  2. 2

    Python multiprocessing takes more time

  3. 3

    Code takes much more time to finish with more than 1 thread

  4. 4

    Curl from php takes more time than curl via putty

  5. 5

    logback: AsyncAppender takes more time than Synchronous FileAppender

  6. 6

    Why using static logger takes more time than not using it

  7. 7

    Background thread takes more time than UI thread

  8. 8

    Kill process if it takes more than xGiB of RAM in a given amount of time

  9. 9

    Python Generator that yields more results takes more time to create

  10. 10

    Python takes six times more memory than it should

  11. 11

    Calculation of a time interval a time list until the next time mark is more than x minutes away

  12. 12

    How to print more than one div at the same time in same pdf?

  13. 13

    can i perform operator overloading of more than 2 objects in python?

  14. 14

    Perform "bigger than" on more than two digits

  15. 15

    Populating table for more than 20k rows it takes lot of time and page becomes unresponsive when it done

  16. 16

    Improving query run time (Takes more than 20seconds to load a page)

  17. 17

    print comma separated if more than two values in the list python

  18. 18

    print more than one output for a particular condition in python

  19. 19

    Java-Calculator more than one calculation

  20. 20

    Calculation for more than one column in data frame

  21. 21

    JBrowserDriver takes more than a minute to initialize

  22. 22

    MySQL statement takes more than minute to execute

  23. 23

    JBrowserDriver takes more than a minute to initialize

  24. 24

    Kubuntu takes more than 2 minutes to boot!

  25. 25

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

  26. 26

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

  27. 27

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

  28. 28

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

  29. 29

    Why does the camera calibration in opencv-python takes more than 30 minutes?

HotTag

Archive