Calculating the time taken by a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    Calculating the time taken by a function

    Hi,

    I would like to know the time taken by each function,for an application.Oth er than profiler ,is there any method/function available to print the duration of execution of method/function.

    Thanks
    PSB
  • dshimer
    Recognized Expert New Member
    • Dec 2006
    • 136

    #2
    If I'm just trying to keep track of something I do something fairly simple

    Code:
    >>> import time
    >>> def testtime(num):
    ... 	start=time.time()
    ... 	for n in range(num):
    ... 		print n
    ... 	print time.time()-start
    ... 	
    >>> testtime(10)
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    0.0499999523163
    Originally posted by psbasha
    Hi,

    I would like to know the time taken by each function,for an application.Oth er than profiler ,is there any method/function available to print the duration of execution of method/function.

    Thanks
    PSB

    Comment

    • psbasha
      Contributor
      • Feb 2007
      • 440

      #3
      Then we have to call time modules ,which ever function we need to call.

      -PSB

      Comment

      • dshimer
        Recognized Expert New Member
        • Dec 2006
        • 136

        #4
        I guess I'm not totally sure what you are asking and could be off base with my comments, there may be other ways of doing this, I just find it easy enough to.
        1) import the time module
        2) Use time.time() to capture the starting time of an event
        3) At the end of the event or function get the current time with time.time() and compute the difference from the beginning.

        Originally posted by psbasha
        Then we have to call time modules ,which ever function we need to call.

        -PSB

        Comment

        Working...