Skip to main content
UseCasePilot

ChatGPT Prompt for Optimizing Python Code

Use this ChatGPT prompt to optimize Python code for performance. Copy the template, paste your code, and get an optimized version with an explanation of each improvement.

Prompt Template

prompt.txt
You are a senior Python engineer specialising in performance optimization.

Analyze the following Python code for performance issues.

Provide:
1. Algorithmic inefficiencies (O(n²) loops, repeated lookups).
2. Unnecessary work inside loops or hot paths.
3. Memory allocations that can be avoided.
4. Opportunities to use built-in optimised methods.

For each suggestion, explain the expected performance impact.

Code:
[PASTE CODE HERE]

How to Use This Prompt

  1. 1Copy the prompt template above.
  2. 2Paste it into ChatGPT at chat.openai.com.
  3. 3Replace [PASTE CODE HERE] with the Python code you want to optimize.
  4. 4Optionally describe the scale of data or the performance target you need to hit.
  5. 5Review each suggestion and apply the ones relevant to your context.

When to Use This Prompt

  • Your Python code is running slower than expected.
  • Profiling shows a hot path that needs improvement.
  • You are scaling up and need to handle larger data volumes.
  • A performance review flagged inefficient patterns in your code.

Example Input

function findDuplicates(arr) {
  const duplicates = []
  for (let i = 0; i < arr.length; i++) {
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[i] === arr[j] && !duplicates.includes(arr[i])) {
        duplicates.push(arr[i])
      }
    }
  }
  return duplicates
}

Expected Output

The AI will identify the O(n²) nested loop and the includes() check adding another O(n) pass, suggest replacing the approach with a Set for O(n) overall complexity, and return an optimised version with comments explaining the improvement.

Recommended AI Tools

Recommended Tool

Free plan

CursorAI-native code editor built for pair programming with LLMs.

Try Cursor

Recommended Tool

Free trial

GitHub CopilotAI pair programmer that suggests code completions in real time.

Try GitHub Copilot

Recommended Tool

Free plan

TabninePrivacy-focused AI code assistant for teams.

Try Tabnine

Related Prompt Templates

Frequently Asked Questions

Can ChatGPT optimize Python code?

Yes. ChatGPT can identify performance bottlenecks in Python code, suggest algorithmic improvements, and return an optimised version with explanations. For best results, provide context about your data volume and performance goals.

What Python performance issues can AI identify?

AI can identify algorithmic inefficiencies, redundant loops, unnecessary memory allocations, blocking I/O in async code, and opportunities to use more efficient built-in methods.

Should I apply AI-suggested Python optimisations directly to production?

No. Always benchmark optimisations in a staging environment and measure the actual improvement before applying changes to production. Some suggestions may not apply to your specific data distribution or access patterns.

What context helps ChatGPT give better Python optimization advice?

Include the scale of your data (record counts, call frequency), any profiling output or slow query logs, and the performance target you need to achieve. This helps the AI prioritise the most impactful changes.