Profiler
import time

def get_name():
    time.sleep(5)
    
def main():
    get_name()
        
time
import time

start = time.time()
main()
end = time.time()

end - start # seconds
		
datetime
import datetime

start = datetime.datetime.now()
main()
end = datetime.datetime.now()

end - start # datetime.timedelta
		
timeit
import timeit

def linear_search(mylist, find):
    for x in mylist:
        if x == find:
            return True
    return False

SETUP_CODE = '''
from __main__ import linear_search
from random import randint'''
 
TEST_CODE = '''
mylist = [x for x in range(10000)]
find = randint(0, len(mylist))
linear_search(mylist, find)'''
     

timer = timeit.Timer(setup = SETUP_CODE, stmt = TEST_CODE)

# execute main statement 1000 times
timer.timeit(number = 1000) # 0.40 second

# automatically determine how many times to call timeit()
timer.autorange() # (1000, 0.397 second)

# repeat 5 times, each time executes statement 1000 times
timer.repeat(repeat=5, number=1000)
        
cProfile
import cProfile

cProfile.run('main()')
        
import pstats
from pstats import SortKey

# save the results to a file
cProfile.run('main()', 'restats')

# printing the results
p = pstats.Stats('restats') # read the result file

# call, call count
# cumulative, cumulative time
# cumtime, cumulative time
# file, file name
# filename, file name
# module, file name
# ncalls, call count
# pcalls, primitive call count
# line, line number
# name, function name
# nfl, name/file/line
# stdname, standard name
# time, internal time
# tottime, internal time
p.sort_stats(SortKey.NAME) # sort by function name

p.print_stats() # print results
        
# -o, output file name
# -s, sort value
# python -m cProfile [-o output_file] [-s sort_order] myscript.py
python -m cProfile -o output script.py
        
Profile Viewer
# gprof2dot
pip install gprof2dot
brew install graphviz # install dot

gprof2dot -f pstats output | dot -Tpng -o output.png
        
# SnakeViz, web-based profiler
pip install snakeviz

snakeviz output
        
# tuna, web-based profiler
pip install tuna

tuna output
        
Jupyter Profiler
%time # time the execution of a single statement
%time main()

%timeit # time repeated execution of a single statement and return average
%timeit main()

%prun # run code with the profiler
%prun main()
        
pip install line_profiler
%load_ext line_profiler

%lprun # run code with the line-by-line profiler
%lprun main()
        
pip install memory_profiler
%load_ext memory_profiler

%memit # measure the memory use of a single statemen
%memit main()

%mprun # run code with the line-by-line memory profiler
# works only for functions defined in separate modules
from p1 import main
%mprun -f main main()
        
Reference