For those looking for / needing a python version
(as pyspark google search leads to this post) :
from time import time
from datetime import timedelta
class T():
def __enter__(self):
self.start = time()
def __exit__(self, type, value, traceback):
self.end = time()
elapsed = self.end - self.start
print(str(timedelta(seconds=elapsed)))
Usage :
with T():
//spark code goes here
As inspired by :
https://blog.usejournal.com/how-to-create-your-own-timing-context-manager-in-python-a0e944b48cf8
Proved useful when using console or whith notebooks
(jupyter magic %%time an %timeit are limited to cell scope, which is inconvenient when you have shared objects across notebook context)