本文へスキップ
Skillpedia
一般的なプログラミング41k · #6764

python-performance-optimization

できること

このツールは、Pythonコードの中で遅い部分を見つけて改善するのに役立ちます。プログラムがどこで最も多くの時間やメモリを使っているかを分析し、より速く効率的にするための方法を提案します。

使う場面

  • ✓Pythonプログラムの実行に時間がかかりすぎているとき。
  • ✓プログラムがメモリを使いすぎていると感じるとき。
  • ✓公開する前に、コードをより速くしたいとき。

スキルのリンク

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

コピーしてAI(Claude、ChatGPT、Cursor)に貼り付け、指示に従うよう頼んでください。

使い方

いちばん簡単な方法:メッセージをコピーしてAIに貼り付けるだけ。AIがスキルを読んで指示に従います。

貼り付け用メッセージ

このスキルを使って手伝ってください:https://github.com/wshobson/agents/blob/main/plugins/python-development/skills/python-performance-optimization/SKILL.md このリンクのSKILL.mdファイルを読んで、私の作業でその指示に従ってください。
こういうスキルを、AIが自分で見つけてくれたら?無料のSkillpedia MCPをインストール。1行で、作業の前に15,000+のスキルから探してくれます。1分でインストール →

AIへの頼み方の例

“Pythonコードの動きがとても遅いのですが、原因を見つけるのを手伝ってもらえますか?”

毎週5つの新しいスキル。1分で読めます。

仕組み

このツールはコードを分析して、実行に最も時間がかかる部分や、最もメモリを消費している部分を調べます。そのうえで、より効率的なデータ構造を使う、より速いアルゴリズムを使うなどの変更を提案します。

技術的な内容(元のSKILL.md)

作者による元のテキスト(英語)をGitHubからそのまま表示しています。

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
よくある質問

「プロファイリング」とは何ですか?

プログラムの実行中の様子をスナップショットのように記録し、どこで時間や資源を使っているかを調べることです。

なぜコードを最適化するのですか?

プログラムをより速く動かし、メモリの使用量を減らし、効率を高めるためです。資源を節約し、利用者の体験も向上します。

このツールを使うには、プログラミングの専門家である必要がありますか?

いいえ。このツールは、問題の特定と解決策の提案に重点を置いているため、専門家でなくても使えるように設計されています。

詳細

作者による元の説明

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

人気

GitHubのスター
41k
ランキング
#6764
ライセンス
MIT
更新日
2026/10/15

このスキルは役に立ちますか?

0

ログイン して投票しましょう。

コメント

  • …

このリポジトリの他のスキル

似ているスキル