Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 拆分由扭曲数组组成的数组_C#_.net_Arrays_Performance - Fatal编程技术网

C# 拆分由扭曲数组组成的数组

C# 拆分由扭曲数组组成的数组,c#,.net,arrays,performance,C#,.net,Arrays,Performance,我有一个传感器,它向我发送一个一维浮点数组,我必须把它分成4个不同的子数组。我的数组表示一个帧,它由1024个渐变组成。每个渐变都有标题和4个通道的数据—我要分割的数据。每个通道有2个浮动,一个用于真实部分,一个用于复杂部分。为了澄清这一点,我附上了一张结构如下的图片: 我需要将这个大阵列分解为4个阵列,每个阵列只有一个通道的数据。这必须尽快完成。我的实现大约需要850毫秒,但遗憾的是这还不够快。到目前为止,我已经编写了下一个代码: IntPtr ptr = (IntPtr)frameInfo.

我有一个传感器,它向我发送一个一维浮点数组,我必须把它分成4个不同的子数组。我的数组表示一个帧,它由1024个渐变组成。每个渐变都有标题和4个通道的数据—我要分割的数据。每个通道有2个浮动,一个用于真实部分,一个用于复杂部分。为了澄清这一点,我附上了一张结构如下的图片:

我需要将这个大阵列分解为4个阵列,每个阵列只有一个通道的数据。这必须尽快完成。我的实现大约需要850毫秒,但遗憾的是这还不够快。到目前为止,我已经编写了下一个代码:

IntPtr ptr = (IntPtr)frameInfo.ptr; // The pointer to the buffer

for (int i = 0; i < nChannels; i++)
{
    channelFrames[i].data = new float[nRamps * nPoints * 2];
}

 for (int ramp = 0; ramp < nRamps; ramp++)
 {
     ptr += (int)rawHeaderSize; // Skip the header

     for (int point = 0; point < nPoints; point++)
     {
          for (int channel = 0; channel < nChannels; channel++)
          {
               Marshal.Copy(ptr, channelFrames[channel].data, (int)(point *2 + ramp*nPoints*2), 2);

               ptr += (sizeof(float) * 2); // Move to the next data                          
          }
     }
}
关于如何更快地执行此操作,您有什么想法吗?

Marshal.Copy可能会成为性能瓶颈,因为它调用非托管代码,而且此调用的成本太高,无法仅复制两个浮点值。必须在项目属性中启用以下使用不安全代码,并且必须使用不安全修饰符修饰方法,以避免使用封送处理。手动复制和复制数据。此外,在通道上进行迭代的内部循环也被展开,因为这样做的一些额外性能增益缺点是代码被硬编码为4个通道

我的测量结果显示,与原始方法相比,性能提高了近10倍

//Pin arrays with channel data in memory and get pointers of these fixed arrays
fixed (float* fixed_ch0ptr = channelFrames[0].data)
fixed (float* fixed_ch1ptr = channelFrames[1].data)
fixed (float* fixed_ch2ptr = channelFrames[2].data)
fixed (float* fixed_ch3ptr = channelFrames[3].data)
{
    //fixed arrays pointer cannot be modified, we must create writable copies ot these pointers
    float* ch0ptr = fixed_ch0ptr;
    float* ch1ptr = fixed_ch1ptr;
    float* ch2ptr = fixed_ch2ptr;
    float* ch3ptr = fixed_ch3ptr;

    //unsafe pointer to array from sensor
    float* floatptr = (float*)ptr;

    for (int ramp = 0; ramp < nRamps; ramp++)
    {
        floatptr = (float*)((byte*)(floatptr) + (int)rawHeaderSize); // Skip the header

        for (int point = 0; point < nPoints; point++)
        {
            //Unrolling loop that iterates over channelFrames can give as some additional performance gains

            //copy channel 0 data
            *ch0ptr++ = *(floatptr++);
            *ch0ptr++ = *(floatptr++);

            //copy channel 1 data
            *ch1ptr++ = *(floatptr++);
            *ch1ptr++ = *(floatptr++);

            //copy channel 2 data
            *ch2ptr++ = *(floatptr++);
            *ch2ptr++ = *(floatptr++);

            //copy channel 3 data
            *ch3ptr++ = *(floatptr++);
            *ch3ptr++ = *(floatptr++);
        }
    }
}

也许您应该尝试一下,您的图表似乎与您的描述或代码不匹配?每个通道有2个浮动,但您的图表显示每个通道有8个浮动?什么是重点,什么是重点?为什么你要一次复制两个浮点数,根据你的图表,是re0+re1,然后是re2+re3?我假设数据是一个浮点数组,因为您给它分配了一个新的浮点[]。@NetMage否,它显示每个通道2个浮点。每个浮点为4字节,因此Re0Re1Re2Re3生成一个浮点,Im0Im1Im2Im3生成另一个浮点。nPoints是4096。Marshal.Copy已将4个字节更改为浮点。我需要将实部和虚部这两个浮点数复制到通道数组中。我知道-每个浮点数代表一个字节。