C# 这是可以考虑的。虽然你说你最多只有80个条目,但我怀疑这是个问题,除非你每秒做这么多次
无论如何,这里有一个使用标准for循环的单一迭代,同时最小化元素查找和结构复制(我希望):C# 这是可以考虑的。虽然你说你最多只有80个条目,但我怀疑这是个问题,除非你每秒做这么多次,c#,multidimensional-array,transformation,performance,C#,Multidimensional Array,Transformation,Performance,无论如何,这里有一个使用标准for循环的单一迭代,同时最小化元素查找和结构复制(我希望): int length=inputVector3.Count; 双精度[,]结果=新的双精度[长度*3,1]; for(int i=0;i
int length=inputVector3.Count;
双精度[,]结果=新的双精度[长度*3,1];
for(int i=0;i
我不确定这是否比其他选项快(主要考虑I+length
和I+length*2
部分的数学);最好只是尝试一下。如果您想要一种“快速/高效”的方法,可以考虑避免Linq的一般枚举。虽然你说你最多只有80个条目,但我怀疑这是个问题,除非你每秒做这么多次
无论如何,这里有一个使用标准for循环的单一迭代,同时最小化元素查找和结构复制(我希望):
int length=inputVector3.Count;
双精度[,]结果=新的双精度[长度*3,1];
for(int i=0;i
我不确定这是否比其他选项快(主要考虑
I+length
和I+length*2
部分的数学);最好试试看。这是一个双[,]
但是它总是双[n,1]
,第二维度是0?(其中n
是X
、Y
和Z
条目的总数)现在,当你说“快速/高效”时,你想在runtme中获得最好的性能,还是最容易维护/读取?这是一个双[,]
但它总是双[n,1]
,第二维度是0?(其中n
是X
、Y
和Z
条目的总数)现在,当你说“快速/高效”时,你想在runtme中获得最好的性能,还是最容易维护/读取?我认为生成的数组应该包含[x1,x2,y1,y2,z1,z2],而不是[x1,y1,z1,x2,y2],是的,在我意识到他需要[x1,x2,y1,y2,z1,z2]之前,我的答案是这样的,这就是为什么我在回答中这样做的原因。@Lily我很乐意将我的多行解决方案简化为一行。然而,正如布兰登·加诺所指出的,我的结果应该是[x1,y1,z1,x2,y2,z2]。啊,我的错误。如果您正在寻找[x1,x2,x3…,y1,y2,y3…],那么我全力支持Corylulu的解决方案。我的答案是[x1,y1,z1,x2,y2,z2…]我相信结果数组应该包含[x1,x2,y1,y2,z1,z2],而不是[x1,y1,z1,z1,x2,y2,z2]。是的,我的答案是这样的,直到我意识到他需要[x1,x2,y1,y2,z1,z2],这就是为什么我在回答中这样做的原因。@Lily我很乐意将我的多行解决方案简化为一行。然而,正如布兰登·加诺所指出的,我的结果应该是[x1,y1,z1,x2,y2,z2]。啊,我的错误。如果您正在寻找[x1,x2,x3…,y1,y2,y3…],那么我全力支持Corylulu的解决方案。我的结果是[x1,y1,z1,x2,y2,z2…]Hm。。。我们将每秒进行多次测试。@IwanKelaiah然后我建议您在实施任何想法之前/之后设置某种性能基准测试方法。准确地识别代码当前的瓶颈并验证这一点很重要。实际上,在这个阶段,性能方面并不重要。我更关心的是我那丑陋而冗长的代码。为了清楚起见,我将修改问题的标题说明。@IwanKelaiah,那没关系。将所有这些逻辑放在它自己的自包含方法中,使其易于阅读/维护(以及您了解它是如何运行的),然后在以后发现瓶颈时对其进行基准测试。。。我们将每秒进行多次测试。@IwanKelaiah然后我建议您在实施任何想法之前/之后设置某种性能基准测试方法。准确地识别代码当前的瓶颈并验证这一点很重要。实际上,在这个阶段,性能方面并不重要。我更关心的是我那丑陋而冗长的代码。为了清楚起见,我将修改问题的标题说明。@IwanKelaiah,那没关系。将所有这些逻辑放在它自己的自包含方法中,使其易于阅读/维护(以及您了解它是如何运行的),然后在以后发现瓶颈时对其进行基准测试。
public class Trajectory
{
public Vector3 Position { get; set; }
// ... more codes
}
public struct Vector3
{
public float X;
public float Y;
public float Z;
public Vector3(float x, float y, float z)
{
X = x;
Y = y;
Z = z;
}
// ... more vector3 operators
}
// take a snapshot of current trajectory
List<Entry> tempEntry = new List<Entry> (Entries);
// create a temporary vector3Values
List<Vector3> vector3Values = new List<Vector3>();
foreach (Entry e in tempEntry)
{
vector3Values.Add(new Vector3(e.Position.X, e.Position.Y, e.Position.Z));
}
/* Start an index at 0.
* This is for foreach iteration to extract the value of x, y, and z from each vector3
*/
int index = 0;
// find the size of the list, in case max limit is changed
int listCount = inputVector3.Count;
/* set the length of the new array by multiplying the size of the list by 3.
* We want:
* [x1 x2 x3...xn y1 y2 y3...yn z1 z2 z3...zn]'.
* Therefore, the size of the reshaped array is three times of the original array *
*/
int maxRowLength = listCount * 3;
// create double[,] variable to store the reshaped data, three times the length of the actual list.
double[,] result = new double[maxRowLength, 1];
// start going for each vector, then store the x components in the double[,] array.
foreach (Vector3 vector3 in inputVector3)
{
result[index, 0] = vector3.X;
index++;
}
/* continuing from the previous index value, start going for each vector,
* then store the z components in the double[,] array.
*/
foreach (Vector3 vector3 in inputVector3)
{
result[index, 0] = vector3.Y;
index++;
}
/* continuing from the previous index value, start going for each vector,
* then store the z components in the double[,] array.
*/
foreach (Vector3 vector3 in inputVector3)
{
result[index, 0] = vector3.Z;
index++;
}
List<Vector3> vectors = new List<Vector3>();
//Select() will isolate the float value [X/Y/Z] of each type from the collection
//Concat() will merge the selected float[]'s to make
// one large array containing [x1,x2,x3...y1,y2,y3...z1,z2,z3...]
float[] values = vectors.Select(e => e.X)
.Concat(vectors.Select(e => e.Y)
.Concat(vectors.Select(e => e.Z))).ToArray();
for (int i = 0; i < values.Length; i++)
{
result[i, 0] = values[i];
}
double[] result = trajectory.SelectMany(t => new double[] { t.Position.X, t.Position.Y, t.Position.Z }).ToArray();
var result = new double[entries.Count * 3, 1];
for (int i = 0; i < entries.Count; i++)
{
result[i, 0] = entries[i].Position.X;
result[i + entries.Count, 0] = entries[i].Position.Y;
result[i + entries.Count * 2, 0] = entries[i].Position.Z;
}
int length = inputVector3.Count;
double[,] result = new double[length * 3, 1];
for (int i = 0; i < length; i++)
{
var vector = inputVector3[i];
result[i, 0] = vector.X;
result[i + length, 0] = vector.Y;
result[i + length * 2, 0] = vector.Z;
}
return result;