Ниже приведены несколько способов добавления тестов с примерами кода:
Метод 1: использование модуля time
import time
start_time = time.time()
# Code to benchmark
end_time = time.time()
execution_time = end_time - start_time
print(f"Execution time: {execution_time} seconds")
Метод 2. Использование модуля timeit
import timeit
def code_to_benchmark():
# Code to benchmark
execution_time = timeit.timeit(code_to_benchmark, number=1)
print(f"Execution time: {execution_time} seconds")
Метод 3. Использование функции perf_counter
import time
start_time = time.perf_counter()
# Code to benchmark
end_time = time.perf_counter()
execution_time = end_time - start_time
print(f"Execution time: {execution_time} seconds")
Метод 4. Использование модуля cProfileдля детального профилирования
import cProfile
def code_to_benchmark():
# Code to benchmark
cProfile.run("code_to_benchmark()")
Метод 5. Использование модуля memory_profilerдля измерения использования памяти
!pip install memory_profiler
import memory_profiler
@profile
def code_to_benchmark():
# Code to benchmark
code_to_benchmark()