返回提示词目录第 1409 页

将数组循环改写为向量化计算

依据数组形状和数据类型优化循环,在校验结果等价的基础上对比耗时与峰值内存。

完整提示词

<context>
A Python loop over array elements runs at Python speed; the same work as one NumPy expression runs in compiled code, often 10 to 100 times faster. Getting there safely takes more than deleting the loop. Broadcasting rules (shapes align from the right, size-1 dimensions stretch) let you combine arrays without copies, but a careless broadcast of two large vectors into a matrix (`a[:, None] - b[None, :]`) can allocate gigabytes; chunking or a different formulation avoids it. Conditional logic becomes `np.where`, boolean masks or `np.select`; cumulative logic becomes `cumsum`, `cumprod` or `np.maximum.accumulate`; group-by style work becomes `np.bincount`, `np.add.at` (not `a[idx] += 1`, which ignores repeated indices) or sorting with `np.unique(return_inverse=True)`. Loops with true sequential dependencies (each step depends on the previous result in a non-linear way) often cannot be vectorised; Numba is the usual answer there. Dtypes matter: `float32` halves memory and may change results, integer overflow wraps silently, and `object` arrays are no faster than lists. Floating-point results can differ in the last bits because operation order changes, so equivalence is checked with `np.allclose` and a stated tolerance, not `==`. Views versus copies matter for both speed and correctness, since in-place operations on views change the original.
</context>

<task>
Vectorise: {{code}}

1. Read the function, its callers, and the shapes and dtypes it receives. Write a benchmark with realistic shapes and record the baseline time and peak memory.
2. Save reference outputs from the original on several inputs, including edge cases: empty arrays, a single element, NaN and inf values, repeated indices, and the largest realistic size.
3. Identify the loop pattern: element-wise maths, conditionals, reductions, cumulative operations, pairwise computations, scatter or group-by, or a true sequential dependency.
4. Rewrite with the matching NumPy tools, keeping intermediate arrays within memory (chunk pairwise work if a full matrix would be too large), choosing dtypes deliberately and avoiding silent overflow.
5. Check equivalence against the saved outputs with `np.allclose` (state `rtol` and `atol`) or exact equality for integers, and run the existing tests.
6. Measure time and memory again. If part of the loop is truly sequential, leave it and say whether Numba would help.
</task>

<constraints>
- Do not change results beyond a stated floating-point tolerance, and never change integer results.
- Do not create intermediates that exceed available memory for the largest realistic input; say what size you checked.
- Do not add Numba, Cython or other dependencies without asking.
- Report only measured speed-ups.
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>

<output_format>
## Baseline
Shapes, dtypes, time and peak memory.
## Vectorised code
The new function in a fenced block, with one line per step on the NumPy technique used.
## Equivalence check
Inputs checked, tolerance, and the real result.
## Results
Table: version, time, peak memory, speed-up.
## Caveats
Dtype, memory or sequential parts left as loops, or "None".
</output_format>

可替换内容

  • {{code}}
  • {{> guardrails/scope-discipline}}
  • {{> guardrails/verify-before-done}}
  • [:, None]
  • [None, :]
  • [idx]

分类与标签

编程开发、数组计算、向量化优化、性能基准、数值校验

前往 AI 图片创作