Existe-t-il un moyen plus performant de concaténer les tableaux 2-d que celui-ci?C# 2-d array concaténation
static void Main(string[] args)
{
int[][] array1 = { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }, new int[] { 7, 8, 9 } } ;
int[][] array2 = { new int[] { 1, 2, 3 }, new int[] { 4, 5, 6 }, new int[] { 7, 8, 9 } };
int[][] array3 = Concat(array1, array2);
}
private static int[][] Concat(int[][] array1, int[][] array2)
{
int array1Length = array1.Length;
int array2Length = array2.Length;
int[][] result = new int[array1Length + array2Length][];
int i = 0;
for (; i < array1Length; i++)
result[i] = array1[i];
for (; i < array2Length + array1Length; i++)
result[i] = array2[i - array1Length];
return result;
}
Edit: Je voudrais savoir si c'est une bonne pratique de la concat profonde du tableau 2d
private static int[][] DeepConcat(int[][] array1, int[][] array2)
{
int array1Length = array1.Length;
int array2Length = array2.Length;
int[][] result = new int[array1Length + array2Length][];
int i = 0;
for (; i < array1Length; i++)
{
result[i] = new int[array1[i].Length];
for (int j = 0; j < array1[i].Length; j++)
{
result[i][j] = array1[i][j];
}
}
for (; i < array2Length + array1Length; i++)
{
result[i] = new int[array2[i - array1Length].Length];
for (int j = 0; j < array2[i - array1Length].Length; j++)
{
result[i][j] = array2[i - array1Length][j];
}
}
return result;
}
Avez-vous identifié cela comme quelque chose qui prend beaucoup de temps de traitement? Si ce n'est pas le cas, laissez-le tranquille jusqu'à ce qu'il le fasse. – ChrisF
Quelle est la performance de la méthode existante? –
la chose est que je vais incorporer ceci à un code très critique et selon mes résultats de test, il fonctionne bien dans la gamme acceptable, je veux juste m'assurer qu'il est le meilleur que je peux faire – mustafabar