Linked topic (but not duplicate): Decorator to time specific lines of the code instead of whole method?
I know how decorators are usually used for Python functions.
Is there a similar concept/syntax for single lines of code?
Example: with
def measuretime(lineofcode):
start = time.time()
lineofcode()
print time.time() - start
then
@measuretime
im = Image.open(BytesIO(base64.b64decode(data)))
would be interpreted like
start = time.time()
im = Image.open(BytesIO(base64.b64decode(data)))
print time.time() - start
Notes:
I know measuring execution time like this is not optimal, it's better to use
timeit
, etc. but it's just a random example to show what I'm looking for (decorators for single lines of code)I'm looking for a 1 or 2 lines of code solution (+ definition of the function of course). If the solution takes more than 2 lines of code (i.e. more than something like
@measuretime
), then it's probably better to give up and just do the normal:start = time.time() im = Image.open(BytesIO(base64.b64decode(data))) print time.time() - start