-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfiling.py
More file actions
56 lines (48 loc) · 1.67 KB
/
Copy pathProfiling.py
File metadata and controls
56 lines (48 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Profiling.py
# Reference Guide: Profiling code execution speed (timeit, cProfile) and memory usage in Python
import timeit
import cProfile
# ==========================================
# 1. TIMEIT (Timing small code snippets)
# ==========================================
# timeit evaluates execution times by running a statement repeatedly.
print("--- 1. TIMEIT SNIPPET TIMING ---")
# Timing list comprehension vs appending to list
time_comprehension = timeit.timeit("[x**2 for x in range(1000)]", number=1000)
time_loop = timeit.timeit(
"""
res = []
for x in range(1000):
res.append(x**2)
""",
number=1000
)
print(f"List comprehension (1000 runs): {time_comprehension:.4f} seconds")
print(f"Loop and append (1000 runs): {time_loop:.4f} seconds")
print()
# ==========================================
# 2. cPROFILE (Performance profiling)
# ==========================================
# cProfile profiles entire program runs, tracking call counts and call durations.
print("--- 2. cPROFILE STATS ---")
def dummy_work():
total = 0
for i in range(100000):
total += i
return total
# Programmatic profiling run
cProfile.run("dummy_work()")
# To profile an entire script from command-line:
# `python -m cProfile -s tottime my_script.py`
print()
# ==========================================
# 3. MEMORY PROFILING
# ==========================================
# Tracking memory footprint line-by-line using `memory_profiler`.
# 1. Install: `pip install memory-profiler`
# 2. Annotate function: `@profile`
# 3. Run: `python -m memory_profiler script.py`
#
# Output shows increment in memory usage (MiB) for each code line.
print("Memory profiling reference configured!")
print()