Decorator to measure execution time of a function This decorator adds the elapsed time to a function's attributes when applied.
My concerns:

Is the code pythonic?
Could this code be useful?
Should I just use the timeit module?
Is the code easy to read and understand?

The code:
'''
:Date: 7/21/17
:Version: 1
:Authors:
    - Ricky L Wilson
'''
 

import datetime

def time_func(function):
  &quot;&quot;&quot; This decorator calculates the amount of time a function takes to execute.
  
  When time_func is applied to a function it records how long the function takes to
  finish and add the elapsed time to the functions attributes.
  
  - **parameters**
  :param function: The function you want to add the elapsed time attribute to.
  
  :Example:
  @time_func  
  def example(name, **kwargs):
    meta = type(name, (object,), kwargs)
    return meta
    
  example('foo')
  print example.elapsed
  0:00:00.000052
  
  &quot;&quot;&quot;
  def new_func(*args, **kwargs):
    # Start the clock.
    start = datetime.datetime.now()
    # Execute the function and record the results.
    function_result = function(*args, **kwargs)
    # Calculate the elapsed time and add it to the function
    # attributes.
    new_func.elapsed = datetime.datetime.now() - start
    # Returned the function with the added elapsed attribute 
    return function_result
  return new_func
I'd recommend the timeit module when measuring the execution time of functions. AFAIK, timeit disables the garbage collector for the duration of the test, which might give you better results overall.

From here:


  timeit is more accurate, for three reasons:
  
  
  it repeats the tests many times to eliminate the influence of other tasks on your machine, such as disk flushing and OS scheduling.
  it disables the garbage collector to prevent that process from skewing the results by scheduling a collection run at an inopportune
  moment.
  it picks the most accurate timer for your OS, time.time or time.clock, see
  timeit.default_timer.
  


On the other side, a timing decorator is really useful because you can use annotations to sprinkle the timing around your code rather than making your code messy with timing logic everywhere. So yes, related to one of your questions, the code is useful.

Now, on the pythonic question, IMO:


you have too many comments which unfortunately didn't add any value to your code. Remove them.
your inner function could also be renamed to something more intuitive like wrapper.
function is also not the best choice when it comes to naming conventions as it might shadow the built-in function
use 2 newlines between imports and methods
the indentation should be a multiple of 4 (spaces)
use triple double quotes for your module docstring




In Python, there're already some useful modules to help you with this. For example, in functools you have the wraps decorator. This takes a function used in a decorator and adds the functionality of copying over the function name, docstring, arguments list, etc. And since wraps is itself a decorator I guess it'll make things a lot easier.



Code:

from functools import wraps
from time import time


def timing(f):
    @wraps(f)
    def wrapper(*args, **kwargs):
        start = time()
        result = f(*args, **kwargs)
        end = time()
        print 'Elapsed time: {}'.format(end-start)
        return result
    return wrapper


Usage:

@timing
def f(a):
    for _ in range(a):
        pass

print(f(2000000))


Result:


  Elapsed time: 0.0971460342407