When an analysis grows from thousands of orders to millions, the same pipeline may no longer deliver results quickly enough. Performance optimization changes the algorithm, code, or data-processing pipeline to improve speed or resource use. It differs from scaling, which adapts code to use more powerful hardware or different architectures. section 7.0 section 7.2
Start with the constraint, not the fastest-looking tool. A self-contained patient-monitoring unit may need efficient code on each device. A financial-market system may be better served by clear, maintainable code and additional server capacity. Neither approach fits every workload. section 7.2 section 7.0
Ask for a change, then check it
A practical prompt is: “Optimize this code, preferably without parallelization. Name the resulting function bootstrap_CI_optimized.” For a bootstrap confidence-interval function, the proposed Python change replaces repeated sampling in a loop with NumPy-generated indices and an array of samples. A reported execution-time comparison showed an almost sevenfold reduction. Treat that as a result to reproduce, not a guarantee: run both versions, compare their outputs, and inspect the generated code before relying on it. section 7.2.1
def bootstrap_CI(data, func, n_bootstraps=1000, ci=95, axis=0):
bootstrapped_statistics = []
for _ in range(n_bootstraps):
bootstrap_sample = np.random.choice(data, size=len(data),
replace=True)
bootstrapped_statistic = func(bootstrap_sample, axis=axis)
bootstrapped_statistics.append(bootstrapped_statistic)
lower_bound = np.percentile(bootstrapped_statistics, (100 - ci) / 2)
upper_bound =
np.percentile(bootstrapped_statistics, 100 - (100 - ci) / 2)
return lower_bound, upper_bound
The baseline function samples and calculates a statistic once per loop iteration; use it as the behavioral reference for a proposed replacement. section 7.2.1
import numpy as np
def bootstrap_CI_optimized(data, func, n_bootstraps=1000, ci=95, axis=0):
data = np.asarray(data)
idx = np.random.randint(0, len(data), size=(n_bootstraps, len(data)))
bootstrap_samples = data[idx]
bootstrapped_statistics = func(bootstrap_samples, axis=axis)
lower_bound = np.percentile(bootstrapped_statistics, (100 - ci) / 2)
upper_bound =
np.percentile(bootstrapped_statistics, 100 - (100 - ci) / 2)
return lower_bound, upper_bound
The proposed replacement generates sample indices together and passes the resulting array to the statistic function. Inspect the function’s output and the listing’s line breaks before executing it. section 7.2.1
Other routes include refactoring, choosing more efficient data structures or libraries, and converting code between languages. Conversion may improve speed, memory use, maintainability, or framework compatibility, but it costs time and can introduce bugs. Check current framework requirements before making that investment. section 7.2.1 section 7.4
Be especially skeptical of an AI assistant’s explanation of why a change is faster. In one Python string-search example, it described a replacement as improving worst-case complexity from quadratic to linear, although the original already used one loop. Read the original code, test representative inputs, and measure performance rather than accepting a confident claim. section 8.3 section 7.2.1






