python|July 30, 2019|1 min read

Implementation of Timeit function, Get performance numbers for a function

TL;DR

Create a Python decorator that wraps any function to measure its execution time using time.time(), enabling easy performance profiling across your codebase.

Implementation of Timeit function, Get performance numbers for a function

This is regarding the timeit implementation in python. The basic requirement comes when you want to see how much time your methods are taking. And, on the basis on those numbers you would want to take some action in optimising them.

You should use it on almost every method, and you can use any graphical tool to get the statistics per method. And, you can identify the area which needs improvement.

Implementation of timeit

Below is one implementation of timeit functionality, which implements it as a decorator.

"""
Decorator for taking performance numbers
"""
import time

def perf(method):
    """
    A decorator for fetching time taken by methods
    """
    def timed(*args, **kw):
        # get start time
        start_time = time.time()
        
        # calling the method
        result = method(*args, **kw)
        
        # get end time
        end_time = time.time()

        stats = {}
        stats['method'] = f'{method.__module__}::{method.__qualname__}'
        stats['timetaken'] = '%2.2f' % int((end_time-start_time)*1000)

        print(f'Performance numbers, method={stats["method"]}, timetaken={stats["timetaken"]}')
        return result

    return timed

How to use it

@perf
def test1():
    print('Im in method test1')
    time.sleep(2);

test1()

# Output
Performance numbers, method=__main__::test1, timetaken=2001.00

You can change logging with whatever suits you. I have used print(), but you can use logger to log this in your standard log stream.

Related Posts

ReactJS - How to use conditionals in render JSX

ReactJS - How to use conditionals in render JSX

Introduction In this post, I will show several ways to use conditionals while…

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

ReactJS - Understanding SetState Asynchronous Behavior and How to correctly use it

ReactJS - Understanding SetState Asynchronous Behavior and How to correctly use it

ReactJS setState is Asynchronous setState() method in ReactJS class components…

Implement a command line shell by using Command Dispatcher in Python

Implement a command line shell by using Command Dispatcher in Python

Lets implement a command shell by using a command dispatcher. The objective is…

How to solve - Apache Ftp Client library is printing password on console

How to solve - Apache Ftp Client library is printing password on console

The problem comes while using FTPS. When developer uses login method of this…

How to get Youtube Video Thumbnail Images

How to get Youtube Video Thumbnail Images

If your youtube video looks like:https://www.youtube.com/watch?v=g0kFl7sBdDQ…

Latest Posts

REST API Design: Pagination, Versioning, and Best Practices

REST API Design: Pagination, Versioning, and Best Practices

Every time two systems need to talk, someone has to design the contract between…

Efficient Data Modelling: A Practical Guide for Production Systems

Efficient Data Modelling: A Practical Guide for Production Systems

Most engineers learn data modelling backwards. They draw an ER diagram…

Deep Dive on Caching: From Browser to Database

Deep Dive on Caching: From Browser to Database

“There are only two hard things in Computer Science: cache invalidation and…

System Design Patterns for Real-Time Updates at High Traffic

System Design Patterns for Real-Time Updates at High Traffic

The previous articles in this series covered scaling reads and scaling writes…

System Design Patterns for Scaling Writes

System Design Patterns for Scaling Writes

In the companion article on scaling reads, we covered caching, replicas, and…

System Design Patterns for Managing Long-Running Tasks

System Design Patterns for Managing Long-Running Tasks

Introduction Some operations simply can’t finish in the time a user is willing…