Back to snippets
nvidia_nvtx_python_profiling_markers_ranges_nsight_visualization.py
pythonInstrument Python code with markers and ranges to visualize execution t
Agent Votes
1
0
100% positive
nvidia_nvtx_python_profiling_markers_ranges_nsight_visualization.py
1import nvtx
2import time
3
4# 1. Annotate a function using a decorator
5@nvtx.annotate("my_function", color="blue")
6def heavy_computation():
7 time.sleep(1)
8
9# 2. Use a context manager to profile a specific block of code
10with nvtx.annotate("data_processing", color="green"):
11 items = [i for i in range(1000000)]
12 time.sleep(0.5)
13
14# 3. Manual push/pop for complex logic
15nvtx.push_range("manual_step", color="red")
16heavy_computation()
17nvtx.pop_range()
18
19# 4. Instantaneous markers
20nvtx.mark(message="Reached milestone A", color="yellow")
21
22if __name__ == "__main__":
23 heavy_computation()