tags:

views:

380

answers:

5

I want to know how much time an import takes for both built-in as well as user defined modules.

+3  A: 

You can test this runnning

$ time python -c "import math"

However, what would this help you? Importing only happens once and will almost never be a bottle neck. Importing the same module over and over will not run significantly slower than importing it once, since Python tracks which modules have already been imported.

What are you actually trying to achieve?

bayer
i have a master script that imports other modules.I need to calculate how much time it takes
@rejinacm: Please update your question with new facts.
S.Lott
A: 

Use profiler: http://docs.python.org/library/profile.html

Anyway, the imports are cached.

vartec
+1  A: 

Use cProfile:

python -m cProfile yourscript.py
vezult
I am using IDLE.I m getting SyntaxError
The command I gave would be what you would run from the commandline, not from within the python interpreter.
vezult
A: 

Tested on Windows in Python 2.4 - you can try it yourself.

>>> import time

>>> ## Built-in module
>>> def testTime():
        now = time.clock() # use time.time() on unix-based systems
        import math
        print time.clock() - now

>>> testTime()
7.54285810167e-006



>>> ## My own module
>>> def testTime():
        now = time.clock()
        import myBuiltInModule # Not its actual name ;-)
        print time.clock() - now

>>> testTime()
0.00253174635324
>>> testTime()
3.70158777141e-006

So there is a big difference between cached modules and those being brought in from scratch. To illustrate, we can reload the module:

>>> def testTime():
        now = time.clock()
        reload(myBuiltInModule )
        print time.clock() - now


>>> testTime()
0.00250017809526
Smashery
+2  A: 

To find out how long an import takes, the simplest way is probably using the timeit module..

>>> import timeit
>>> t = timeit.Timer('import urllib')
>>> t.timeit(number = 1000000)
0.98621106147766113

So to import urllib 1 million times, it took just under a second (on a Macbook Pro)..

i have a master script that imports other modules.I need to calculate how much time it takes

If you mean the total script execution time, on Linux/OS X/Cygwin, you can run the script using the time command, for example:

$ time python myscript.py 

real    0m0.046s
user    0m0.024s
sys     0m0.020s

(remember that includes all the Python interpreter startup time, as well as the actual code execution time, although it's pretty trivial amount)

Another, possibly more useful way is to profile the script:

Instead of running your code with

$ python myscript.py

..you use..

$ python -m cProfile myscript.py
         1059 function calls in 0.015 CPU seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.002    0.002    0.015    0.015 myscript.py:1(<module>)
   [...]

I don't find the command line output very easy to read, so I almost always use gprof2dot, which turns the profiling information into a pretty graphviz graph:

$ python -m cProfile -o myscript.prof myscript.py
$ python gprof2dot.py -o myscript.dot -f pstats myscript.prof
$ dot -Tpng -o profile.png prof_runtest.dot -Gbgcolor=black

Example output (1429x1896px PNG)

dbr