如何有效地从双数组复制到字节数组 [英] How to efficiently copy from a double array to a byte array
问题描述
我正在尝试处理一个包含超过100万个条目的双数组,并尝试了下面列出的以下例程。但是我经历了很长的处理时间,例如1000点需要花费5分钟来处理。这是我为执行此任务而构建的代码片段。从double []转换为byte []时,是否有更有效的方法加速?
double [] val ;
byte [] rawDataByte = new byte [val.Rows];
for(int i = 0; i< val。行; i ++)
{
rawDataByte [i] =(byte)val.Vector [i];
}
先谢谢。
作为谢尔盖告诉你,你展示的代码甚至不会编译,至少在C#中。
最好,最有效的方法是使用缓冲区
类。
int count = 10 跨度>;
double [] reals = new double < /跨度> [数];
随机r = 新 Random();
for ( int i = 0 ; i < count; i ++){
reals [i] = r.NextDouble()* 2000000000 。 0 ;
}
count<< = 3 ; // 与count * = 8相同 - 因为double是64位,因此8字节
byte [] result = new byte [计数];
Buffer.BlockCopy(实数, 0 ,结果, 0 ,count);
这会将双数组的核心位复制到字节数组,按 little endian order 。
如果需要它以big endian顺序,你需要:
- 在Buffer.BlockCopy之后是否生成
。result.Reverse()
()
- 或实现自己的复制功能。
缓冲区块块方法 [ ^ ]
请在发布问题之前,说实话,并显示一些实际编译代码。
我更喜欢Buffer.BlockCopy检查此链接
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.
As Sergey told you, the code you show won't even compile, at least in C#.
The best, most efficient method is to use theBuffer
class.
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 aresult.Reverse()
afterBuffer.BlockCopy()
.
- or implement your own copy function.
Buffer.BlockCopy Method[^]
Please, before posting a question, be honest and show some actually compiling code.
i would prefer Buffer.BlockCopy check this link
这篇关于如何有效地从双数组复制到字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!