You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于SIMD向量高效拆分填充多数组?(性能优化+低分配)

高性能拆分Vector256数组分量

在性能敏感场景下,需将Vector256<double>[]类型的输入数组拆分,把每个向量的X、Y、Z、W分量分别填充到独立的double[]数组X、Y、Z、W中。

已尝试的方案

  • Linq:速度慢且存在额外内存分配;
  • Parallel.For:速度提升一倍,但仍有额外分配,且这两种方案的额外分配无法通过内存池复用。

输入示例代码

Vector256<double>[] input = GetVectors();
double[] X, Y, Z, W;

// how do I fill X, Y, Z, W with their respective values in a performant way?

基准测试代码

[Benchmark]
public double[][] Linq()
{
    var result = new double[4][];

    result[0] = _array.Select(x => x.X).ToArray();
    result[1] = _array.Select(x => x.Y).ToArray();
    result[2] = _array.Select(x => x.Z).ToArray();
    result[3] = _array.Select(x => x.W).ToArray();

    return result;
}

[Benchmark(Baseline = true)]
public double[][] ParallelFor()
{
    var result = new double[][] 
    { 
        new double[Count], 
        new double[Count], 
        new double[Count],
        new double[Count]
    };

    Parallel.For(0, Count, i =>
    {
        result[0][i] = _array[i].X;
        result[1][i] = _array[i].Y;
        result[2][i] = _array[i].Z;
        result[3][i] = _array[i].W;
    });

    return result;
}

基准测试结果

方法Count均值误差标准差比值比值标准差Gen0Gen1Gen2分配内存分配比值
Linq100013.259 us0.1522 us0.1271 us1.470.0210.2997--31.59 KB0.94
ParallelFor10008.993 us0.0837 us0.0699 us1.000.0011.1389--33.58 KB1.00
Linq10000136.482 us2.7235 us3.8179 us2.940.0967.382833.4473-312.84 KB0.99
ParallelFor1000046.989 us0.6139 us0.5742 us1.000.0075.073236.0718-314.83 KB1.00
Linq1000001,877.981 us18.9144 us15.7944 us1.920.11562.5000562.5000562.50003125.51 KB1.00
ParallelFor100000992.898 us19.7800 us50.7037 us1.000.00260.7422256.8359256.83593130.72 KB1.00

优化目标

希望通过非托管内存操作或SIMD优化技术,实现更低内存分配、更高性能的拆分填充方案。


内容的提问来源于stack exchange,提问作者JustNrik

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 02:14:53