0

currently i'm using the below code to find elapsed time for any routine/functions that i manually use start time and end time all that

But, i want a fuction should automatically return the entire function took how long as if like when we run query and it use to say how many seconds and milli seconds like that i want to run any function and each function should return me with that elapsed time.

and here is my code and absolutely i'm not happy with this and i want a high end professional line of code's pls help. Thanks.

def ElaspedTime(stime,etime):
    from datetime import timedelta
    timediff = timedelta(seconds=etime - stime)
    return timediff

def STime():
    return time.monotonic()

def ETime():
    return time.monotonic()

#start_time = time.monotonic()

##call functions here

#end_time = time.monotonic()

#print( ElaspedTime(start_time,end_time))
MooingRawr
  • 4,901
  • 3
  • 24
  • 31
  • Define professional? Also how else are you suppose to calculate a function's run time without having a start point, an end point, and a calculation called..? – MooingRawr Jun 13 '17 at 15:21
  • As a side note, next time, highlight your code, and hit `ctrl`+`k`. – MooingRawr Jun 13 '17 at 15:22
  • long since answered https://stackoverflow.com/questions/7370801/measure-time-elapsed-in-python – Phil Cooper Jun 13 '17 at 15:28
  • 2
    Possible duplicate of [Measure time elapsed in Python?](https://stackoverflow.com/questions/7370801/measure-time-elapsed-in-python) – Phil Cooper Jun 13 '17 at 15:28
  • Thanks buddy...Prof --> Not too childish kind. and i have found some function for time and just say @timefunction before the call of any function it throw that elapsed time...i look for something like that – John Ebenezer Jun 13 '17 at 15:36
  • @PhilCooper absolutely not of this type i want one like the below runwithtime or @timefunction(functionname) like that – John Ebenezer Jun 13 '17 at 15:38
  • 1
    Possible duplicate of [how to measure execution time of functions (automatically) in Python](https://stackoverflow.com/questions/2245161/how-to-measure-execution-time-of-functions-automatically-in-python) – Thomas Kühn Jun 13 '17 at 17:53

2 Answers2

0

You can create a wrapper function like this:

def runWithTime(f,*args):
    from datetime import timedelta
    import time
    stime = time.time()
    r = f(*args)
    etime = time.time()
    timediff = timedelta(etime - stime)
    print timediff
    return r

def a(x,y):
    return x+y

r = runWithTime(a,1,2)
print r
RaphaMex
  • 2,781
  • 1
  • 14
  • 30
  • Good and great idea bro...Thanks for the code but still i look for the proper one and this one is giving like this for 4:16:54.834595 a four second job...i saw some method of using prior to the function as @timefunction – John Ebenezer Jun 13 '17 at 15:40
0

write a decorator as follow (replace log.debug with print ...):

from functools import wraps
def with_stopwatch(f):
    @wraps(f)
    def wrapped(*args, **kw):
        tstart = time()
        result = f(*args, **kw)
        tend = time()
        log.debug('>>> func:%r took: %2.4f sec' % (f.__name__, tend - tstart))
        return result
    return wrapped

@with_stopwatch
def function()
    ...
farax
  • 131
  • 7