0 Replies - 1916 Views - Last Post: 05 May 2012 - 07:01 PM

#1 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 830
  • View blog
  • Posts: 2,251
  • Joined: 23-December 08

Timing a function

Posted 05 May 2012 - 07:01 PM

Description: Pass the function name that you want to time as the first parameter. If that function requires any arguments, just pass them into the calculateRunTime function. For example calculateRunTime(bubbleSort, listToSort) or calculateRunTime(someFunction, parameter1,parameter2) or calculateRunTime(functionWithoutParameters)This is a generic function used to time another function in Python. It returns the time it took for the function to run and the results of the function if there are any.
def calculateRunTime(function, *args):
    """run a function and return the run time and the result of the function.
 If the function requires arguments, pass those in after the function object"""
    startTime = time.time()
    result = function(*args)
    return time.time() - startTime, result


Is This A Good Question/Topic? 0
  • +

Replies To: Timing a function

#2 atraub   User is offline

  • Pythoneer
  • member icon

Reputation: 830
  • View blog
  • Posts: 2,251
  • Joined: 23-December 08

Re: Timing a function

Posted 05 May 2012 - 07:01 PM

Description: Pass the function name that you want to time as the first parameter. If that function requires any arguments, just pass them into the calculateRunTime function. For example calculateRunTime(bubbleSort, listToSort) or calculateRunTime(someFunction, parameter1,parameter2) or calculateRunTime(functionWithoutParameters)This is a generic function used to time another function in Python. It returns the time it took for the function to run and the results of the function if there are any.
import time

def calculateRunTime(function, *args):
    """run a function and return the run time and the result of the function.
 If the function requires arguments, pass those in after the function object"""
    startTime = time.time()
    result = function(*args)
    return time.time() - startTime, result

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1