DEV Community

syncore
syncore

Posted on

How to write Python that runs 10x faster with these tricks

3 min read · 539 words

Python is loved for its readability, but let's be honest: raw execution speed isn't its strong suit. When your script starts crawling, reaching for C extensions or rewriting everything in Go feels like overkill.

What if you could leverage modern AI agents to optimize your codebase, refactor bottlenecks, and implement parallel processing patterns automatically? Let's look at how to use Claude to supercharge your Python performance.

1. Identify and Refactor Algorithmic Bottlenecks

Most Python slowdowns aren't actually Python's fault—they're algorithmic. Nested loops ($O(n^2)$), redundant database calls inside loops, and misuse of data structures will kill performance no matter what you do.

Instead of hunting for these manually, you can have claude-opus-5 analyze your script and rewrite it using efficient idioms (like generator expressions, itertools, and vectorized operations).

Here is how you can build a quick performance-audit script using the official Anthropic Python SDK:

import anthropic

client = anthropic.Anthropic()

slow_code = """
def find_common_elements(list1, list2):
    common = []
    for item1 in list1:
        for item2 in list2:
            if item1 == item2:
                if item1 not in common:
                    common.append(item1)
    return common
"""

response = client.messages.create(
    model="claude-opus-5",
    max_tokens=16000,
    output_config={"effort": "high"},
    messages=[{
        "role": "user",
        "content": f"Optimize this Python code for maximum speed and explain the changes:\n\n{slow_code}"
    }]
)

print(response.content[0].text)
Enter fullscreen mode Exit fullscreen mode

Notice that we use output_config={"effort": "high"} to give the model room to reason through complex algorithmic transformations without messing with deprecated parameters like temperature.

2. Leverage Concurrency for I/O Bound Tasks

If your Python code is waiting on network requests, file I/O, or API calls, standard synchronous execution is your primary bottleneck. Shifting to asyncio or concurrent.futures can yield an instant 10x speedup.

When dealing with asynchronous code, boilerplate can get messy. Let Claude write the async wrapper for you. Here is how to generate a concurrent batch processor using claude-sonnet-5 for high-volume tasks:

import anthropic

client = anthropic.Anthropic()

prompt = """
Write a production-ready Python script using `asyncio` and `httpx` that fetches 
a list of 100 URLs concurrently with a concurrency limit of 10. Include proper 
error handling and type hints.
"""

response = client.messages.create(
    model="claude-sonnet-5",
    max_tokens=16000,
    thinking={"type": "adaptive"},
    messages=[{"role": "user", "content": prompt}]
)

print(response.content[0].text)
Enter fullscreen mode Exit fullscreen mode

By enabling adaptive thinking (thinking={"type": "adaptive"}), the model plans out the edge cases—such as rate limits and connection pooling—before spitting out the final implementation.

3. Enforce Structured Outputs for Data Pipelines

When writing high-performance data processing scripts, parsing messy text or unstructured outputs wastes precious CPU cycles. Use structured outputs to ensure your AI-driven pipelines communicate cleanly with downstream systems.

import anthropic

client = anthropic.Anthropic()

response = client.messages.create(
    model="claude-haiku-4-5",
    max_tokens=4000,
    output_config={
        "format": {
            "type": "json_schema",
            "schema": {
                "type": "object",
                "properties": {
                    "optimized_code": {"type": "string"},
                    "estimated_speedup": {"type": "string"},
                    "bottleneck_identified": {"type": "string"}
                },
                "required": ["optimized_code", "estimated_speedup", "bottleneck_identified"]
            }
        }
    },
    messages=[{
        "role": "user", 
        "content": "Analyze this snippet: sum([i**2 for i in range(10000000)])"
    }]
)

print(response.content[0].text)
Enter fullscreen mode Exit fullscreen mode

Wrapping Up

Writing fast Python isn't about guessing tricks—it's about applying the right paradigms (like concurrency, vectorization, and clean algorithms) directly to your bottlenecks. By integrating the Anthropic API into your local tooling, you can automate code reviews and performance refactors instantly.

What is the slowest script in your codebase right now? Drop it into a Claude prompt and see how much faster it runs. Happy coding!

Top comments (0)