0

Is it possible to convert a short[] to Single[] without using a for-loop?

What I am doing now is:

 Single[] VectorOfSingleValues= new Single[VectorLength];
 ushort[] VectorOfShortValues= new ushort[VectorLength];

for (int i = 0; i < VectorLength; i++)
{
 VectorOfSingleValues[i] = VectorOfShortValues[i];
}

is there any faster way???

Thank you.

Spyros
  • 261
  • 2
  • 14
  • 4
    Why do you think this isn't fast _enough_ ? – Soner Gönül Nov 11 '15 at 15:04
  • 3
    How about `Array.Copy(VectorOfShortValues,VectorOfSingleValues,VectorLength);`? – user4003407 Nov 11 '15 at 15:05
  • 1
    **But is it faster?** – Lasse V. Karlsen Nov 11 '15 at 15:19
  • @PetSerAl While this is indeed faster (for large arrays, usually), it still uses a for loop. If you wanted to be pedantic :P – Luaan Nov 11 '15 at 15:19
  • It doesn't look faster to me, looks like Array.Copy takes about 2.5x the time that the simple loop does. – Lasse V. Karlsen Nov 11 '15 at 15:23
  • I am going to assume here that "faster" in context of this question actually means "less code", in which case both a LINQ-solution and Array.Copy is fine. It isn't **faster** in sense of time spent waiting for the code to complete but that may be completely irrelevant. – Lasse V. Karlsen Nov 11 '15 at 15:27
  • Actually the code that i was lookin for is : VectorOfSingleValues= Array.ConvertAll(VectorOfShortValues, Convert.ToSingle); which is indeed 2 times faster than the for. The for loop in my machine takes 21ms whereas the Array.Convert takes only 10 ms... – Spyros Nov 11 '15 at 15:45

0 Answers0