相关疑难解决方法(0)

有没有更快的方法在C#中复制数组?

我有三个阵列需要组合在一个三维数组中.以下代码显示Performance Explorer中的性能降低.有更快的解决方案吗?

for (int i = 0; i < sortedIndex.Length; i++) {
    if (i < num_in_left)
    {    
        // add instance to the left child
        leftnode[i, 0] = sortedIndex[i];
        leftnode[i, 1] = sortedInstances[i];
        leftnode[i, 2] = sortedLabels[i];
    }
    else
    { 
        // add instance to the right child
        rightnode[i-num_in_left, 0] = sortedIndex[i];
        rightnode[i-num_in_left, 1] = sortedInstances[i];
        rightnode[i-num_in_left, 2] = sortedLabels[i];
    }                    
}
Run Code Online (Sandbox Code Playgroud)

更新:

我其实是在尝试做以下事情:

//given three 1d arrays
double[] sortedIndex, sortedInstances, sortedLabels;
// copy them over to a 3d array (forget about …
Run Code Online (Sandbox Code Playgroud)

c# arrays copy

32
推荐指数
4
解决办法
5万
查看次数

在C#中尽可能快地将数组复制到struct数组

我正在使用Unity 4.5,将图像作为字节数组(每个字节代表一个通道,每个像素占用4个字节(rgba)并在Color32使用此循环将数组转换为数组的纹理上显示它们:

   img = new Color32[byteArray.Length / nChannels]; //nChannels being 4
   for (int i=0; i< img.Length; i++) {
        img[i].r = byteArray[i*nChannels];
        img[i].g = byteArray[i*nChannels+1];
        img[i].b = byteArray[i*nChannels+2];
        img[i].a = byteArray[i*nChannels+3];
    }
Run Code Online (Sandbox Code Playgroud)

然后,使用以下方法将其应用于纹理:

tex.SetPixels32(img);
Run Code Online (Sandbox Code Playgroud)

但是,这会显着降低应用程序的速度(此循环在每一帧上执行),我想知道是否有其他方法可以加快复制过程.我找到了一些人(Color32 []数组的快速拷贝到byte []数组)使用这些Marshal.Copy函数来进行反向处理(Color32到字节数组),但是我还没有能够复制一个字节数组到Color32数组.有人知道更快的方式吗?

先感谢您!

c# arrays marshalling unity-game-engine

7
推荐指数
2
解决办法
4786
查看次数

标签 统计

arrays ×2

c# ×2

copy ×1

marshalling ×1

unity-game-engine ×1