Chuyển đến nội dung
Skillpedia
Lập trình chung41k · #6764

python-performance-optimization

Skill này làm gì

Công cụ này giúp tìm và sửa các phần chậm trong code Python. Nó phân tích xem chương trình dành nhiều thời gian và bộ nhớ nhất ở đâu, rồi đề xuất cách làm nó nhanh và hiệu quả hơn.

Khi nào nên dùng

  • ✓Khi chương trình Python của bạn chạy quá lâu.
  • ✓Khi bạn nghi ngờ chương trình dùng quá nhiều bộ nhớ.
  • ✓Khi bạn muốn tối ưu code trước khi triển khai.

Liên kết skill

https://github.com/wshobson/agents/blob/main/plugins/python-development/skills/python-performance-optimization/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/wshobson/agents/blob/main/plugins/python-development/skills/python-performance-optimization/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Code Python của tôi chạy rất chậm, bạn có thể giúp tìm vấn đề không?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Công cụ phân tích code để xem phần nào mất nhiều thời gian thực thi nhất hoặc tiêu tốn nhiều bộ nhớ nhất. Sau đó, nó đề xuất thay đổi, như dùng cấu trúc dữ liệu hiệu quả hơn hoặc thuật toán nhanh hơn.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

Python Performance Optimization

Comprehensive guide to profiling, analyzing, and optimizing Python code for better performance, including CPU profiling, memory optimization, and implementation best practices.

When to Use This Skill

  • Identifying performance bottlenecks in Python applications
  • Reducing application latency and response times
  • Optimizing CPU-intensive operations
  • Reducing memory consumption and memory leaks
  • Improving database query performance
  • Optimizing I/O operations
  • Speeding up data processing pipelines
  • Implementing high-performance algorithms
  • Profiling production applications

Core Concepts

1. Profiling Types

  • CPU Profiling: Identify time-consuming functions
  • Memory Profiling: Track memory allocation and leaks
  • Line Profiling: Profile at line-by-line granularity
  • Call Graph: Visualize function call relationships

2. Performance Metrics

  • Execution Time: How long operations take
  • Memory Usage: Peak and average memory consumption
  • CPU Utilization: Processor usage patterns
  • I/O Wait: Time spent on I/O operations

3. Optimization Strategies

  • Algorithmic: Better algorithms and data structures
  • Implementation: More efficient code patterns
  • Parallelization: Multi-threading/processing
  • Caching: Avoid redundant computation
  • Native Extensions: C/Rust for critical paths

Quick Start

Basic Timing

import time

def measure_time():
    """Simple timing measurement."""
    start = time.time()

    # Your code here
    result = sum(range(1000000))

    elapsed = time.time() - start
    print(f"Execution time: {elapsed:.4f} seconds")
    return result

# Better: use timeit for accurate measurements
import timeit

execution_time = timeit.timeit(
    "sum(range(1000000))",
    number=100
)
print(f"Average time: {execution_time/100:.6f} seconds")

Detailed patterns and worked examples

Detailed pattern documentation lives in references/details.md. Read that file when the navigation tier above is insufficient.

Best Practices

  1. Profile before optimizing - Measure to find real bottlenecks
  2. Focus on hot paths - Optimize code that runs most frequently
  3. Use appropriate data structures - Dict for lookups, set for membership
  4. Avoid premature optimization - Clarity first, then optimize
  5. Use built-in functions - They're implemented in C
  6. Cache expensive computations - Use lru_cache
  7. Batch I/O operations - Reduce system calls
  8. Use generators for large datasets
  9. Consider NumPy for numerical operations
  10. Profile production code - Use py-spy for live systems

Common Pitfalls

  • Optimizing without profiling
  • Using global variables unnecessarily
  • Not using appropriate data structures
  • Creating unnecessary copies of data
  • Not using connection pooling for databases
  • Ignoring algorithmic complexity
  • Over-optimizing rare code paths
  • Not considering memory usage
Câu hỏi thường gặp

“Profiling” là gì?

Đó giống như chụp một ảnh chương trình khi nó đang chạy để xem nó dành thời gian và tài nguyên ở đâu.

Tại sao phải tối ưu code?

Để chương trình chạy nhanh hơn, dùng ít bộ nhớ hơn và hiệu quả hơn, tiết kiệm tài nguyên và cải thiện trải nghiệm người dùng.

Tôi có phải là lập trình viên chuyên nghiệp để dùng công cụ này không?

Không, công cụ được thiết kế để giúp ngay cả người không chuyên, bằng cách tập trung vào việc xác định vấn đề và đề xuất giải pháp.

Chi tiết

Mô tả gốc của tác giả

Profile and optimize Python code using cProfile, memory profilers, and performance best practices. Use when debugging slow Python code, optimizing bottlenecks, or improving application performance.

repo: wshobson/agents

Độ phổ biến

Sao GitHub
41k
Vị trí xếp hạng
#6764
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự