views:

105

answers:

3

Hi

I would like to know that how much time a particular function has spent during the duration of the program which involves recursion, what is the best way of doing it?

Thank you

+9  A: 

The best way would be to run some benchmark tests (to test individual functions) or Profiling (to test an entire application/program). Python comes with built-in Profilers.

Alternatively, you could go back to the very basics by simply setting a start time at the beginning of the program, and, at the end of the program, subtracting the current time from the start time. This is basically very simple Benchmarking.

Here is an implementation from the an answer from the linked question:

import time
start = time.time()
do_long_code()
print "it took", time.time() - start, "seconds."

Python has something for benchmarking included in its standard library, as well.

From the example give on the page:

def test():
    "Time me"
    L = []
    for i in range(100):
        L.append(i)

if __name__=='__main__':
    from timeit import Timer
    t = Timer("test()", "from __main__ import test")
    print t.timeit()
Justin L.
+1 for profiling
David Zaslavsky
+3  A: 

Use the profiler!

python -m cPython -o prof yourscript.py
runsnake prof

runsnake is a nice tool for looking at the profiling output. You can of course use other tools.

More on the Profiler here: http://docs.python.org/library/profile.html

Will
A: 

Hi Justin

I understand that, I am using cProfile.runctx method and when I see the profiler output using pstats it say that the time is in CPU seconds which is not the real time the program took to run and I want to know the real time each function took, how can I do that?

Thank you

Thank you for your replies but I want to know one thing

Profiling output is based on CPU seconds, not the actual time and they both are different so I thought that would not be very useful

for eg.

db access time build access time perforce access time time taken(w/0 cache)

0.02 1.12 0.27 2m3.017s

The first 3 values are CPU seconds and last one is actual time, so that wont tell how much actual time db access took for example

please pour in your ideas

Thank you

learner
A proper profiler will tell you what functions took how long. Try to use one that does; which one are you using right now?Also, you can always edit your own question with this new information, so that it can show up at the top of the page =)
Justin L.