Python 3 - Format String fun
This post is dedicated for cases where we intend to append a variable value in a…
July 30, 2019
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.
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
@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.
This post is dedicated for cases where we intend to append a variable value in a…
Introduction Lets take a look at how forms are being handled in ReactJS. We will…
Introduction You already have a content type with one or more fields in it…
Introduction I have created a view, with some filters and content fields. I will…
Curl is a wonderful tool for initiate REST APIs or calls. Or, you can literally…
You are developing a nodejs web application having some UI and backend APIs…
Introduction This post has the complete code to send email through smtp server…
Introduction In a normal email sending code from python, I’m getting following…
Introduction In one of my app, I was using to talk to . I have used some event…
Introduction So you have a Django project, and want to run it using docker image…
Introduction It is very important to introduce few process so that your code and…
Introduction In this post, we will see a sample Jenkin Pipeline Groovy script…