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
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
- 1Copy the prompt template above.
- 2Paste it into ChatGPT at chat.openai.com.
- 3Replace [PASTE CODE HERE] with the Python code you want to optimize.
- 4Optionally describe the scale of data or the performance target you need to hit.
- 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 planCursor — AI-native code editor built for pair programming with LLMs.
Recommended Tool
Free trialGitHub Copilot — AI pair programmer that suggests code completions in real time.
Recommended Tool
Free planTabnine — Privacy-focused AI code assistant for teams.
Related Prompt Templates
ChatGPT Prompt for Debugging JavaScript Code
Use this ChatGPT prompt template to debug JavaScript code faster. Copy the prompt, paste it into ChatGPT, and get a clear explanation of the bug plus a corrected version.
ChatGPT Prompt for Debugging React Code
Use this ChatGPT prompt template to debug React code faster. Copy the prompt, paste it into ChatGPT, and get a clear explanation of the bug plus a corrected version.
ChatGPT Prompt for Debugging Node.js Code
Use this ChatGPT prompt template to debug Node.js code faster. Copy the prompt, paste it into ChatGPT, and get a clear explanation of the bug plus a corrected version.
ChatGPT Prompt for Debugging SQL Code
Use this ChatGPT prompt template to debug SQL code faster. Copy the prompt, paste it into ChatGPT, and get a clear explanation of the bug plus a corrected version.
ChatGPT Prompt for Refactoring Python Code
Use this ChatGPT prompt to refactor Python code. Copy the template, paste your code, and get a cleaner, more maintainable version with an explanation of each change.
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.