Click here to Skip to main content
15,884,007 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to process a double array that has over 1 million entries and tried the following routine listed below. However I experienced a very long processing time, which for example a 1000 points took over 5 minutes to process. Here is a snippet of the code I have constructed to perform this task. Is there a more efficient way speed things up when converting from a double[] to a byte[]?


double[] val;
byte[] rawDataByte = new byte[val.Rows];

for (int i = 0; i < val.Rows; i++)
{

rawDataByte[i] = (byte)val.Vector[i];
}


Thanks In Advance.
Posted
Comments
Sergey Alexandrovich Kryukov 28-May-14 19:10pm    
What are you writing here? "val" does not have the member "Rows"; "val.Rows" won't compile. Please, always post some really working code.
I would first of all think why do you have this type cast. Most of your time is spend on type cast.
—SA
phil.o 28-May-14 19:45pm    
Does not have a 'Vector' property, either :)
Sergey Alexandrovich Kryukov 28-May-14 20:58pm    
I am trying to guess: where could it come from? Gibberish...
—SA
Lino S 28-May-14 21:14pm    
Thank You Sergey and Philo for your comments. Yes you are both correct. Basically the code I have written was to work with MATLAB. I'm receiving data from MATLAB via IntPtr reference. From that reference, I am converting it to data type of double[]. I now have to convert this data to a byte array. My real question was focused more on how to efficiently convert data of type double array to byte array. The for loop listed above takes a long time when casting from double to byte. I hope this helps. Thank Again.

As Sergey told you, the code you show won't even compile, at least in C#.

The best, most efficient method is to use the Buffer class.
C#
int count = 10;
double[] reals = new double[count];
Random r = new Random();
for (int i = 0; i < count; i++) {
   reals[i] = r.NextDouble() * 2000000000.0;
}
count <<= 3; // same as count *= 8 - because a double is 64 bits, thus 8 bytes
byte[] result = new byte[count];
Buffer.BlockCopy(reals, 0, result, 0, count);

This will copy the core bits of the double array to the byte array, in little endian order.
If you need it in big endian order, you need to:
- whether make a result.Reverse() after Buffer.BlockCopy().
- or implement your own copy function.

Buffer.BlockCopy Method[^]

Please, before posting a question, be honest and show some actually compiling code.
 
Share this answer
 
v4
Comments
Sergey Alexandrovich Kryukov 28-May-14 21:01pm    
Did you test it? If it was different integer types, it would work, but floating-point layout is very different. And a double is not guaranteed to fit in byte. I would think at not having those doubles in first place or not using bytes...
—SA
phil.o 29-May-14 5:15am    
Never had to, but I know it is possible. It just fits the double's bit representation into a byte array.
From the MSDN page:
"Buffer only affects arrays of primitive types; this class does not apply to objects. Each primitive type is treated as a series of bytes without regard to any behavior or limitation associated with the primitive type.
Buffer provides methods to copy bytes from one array of primitive types to another array of primitive types, get a byte from an array, set a byte in an array, and obtain the length of an array. This class provides better performance for manipulating primitive types than similar methods in the System.Array class.
Buffer is applicable to the following primitive types: Boolean, Char, SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Single, and Double."

After all, a floating point number still has a fixed binary reprentation internally. What to do with it is a totally different story :)
Sergey Alexandrovich Kryukov 29-May-14 9:36am    
That's exactly the thing: this is a matter of concrete types. You are actually doing a sort of reinterpret typecast, with shift. It may or may not work. What works on 2s-complement may not work for floating-point and 2s-complement (okay, positive bytes).
And in all cases, if double is 2.11*e4, no "conversion" to byte can work. :-)
—SA
i would prefer Buffer.BlockCopy check this link
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900