Junior — Middle
What methods can be used to determine how effectively a program or a piece of code is working?
sobes.tech AI
Answer from AI
Several methods are used to evaluate the effectiveness of a program or code:
- Profiling — measuring the execution time of functions and resource usage (CPU, memory). In Python, you can use the
cProfilemodule:
import cProfile
def my_function():
# code
pass
cProfile.run('my_function()')
- Timing — measuring the execution time of a specific code segment using the
timeortimeitmodule:
import time
start = time.time()
# code
end = time.time()
print(f"Execution time: {end - start} seconds")
-
Memory usage analysis — using libraries like
memory_profilerortracemalloc. -
Logging and monitoring — collecting metrics in real-time to assess performance in production.
These methods help identify bottlenecks and optimize code.