python performance timeit - ghdrako/doc_snipets GitHub Wiki

python -m timeit "total = sum(range(1000))"
from timeit import timeit
setup = 'from datetime import datetime'
statement = 'datetime.now()'
result = timeit(setup=setup, stmt=statement)
print(f'Took an average of {result}ms')

Profilowanie CPU

import random
import time
def an_expensive_function():
  execution_time = random.random() / 100
  time.sleep(execution_time)
  if __name__ == '__main__':
    for _ in range(1000):
      an_expensive_function()
python -m cProfile --sort cumtime cpu_profiling.py