如何有效地从双数组复制到字节数组 [英] How to efficiently copy from a double array to a byte array

查看:79
本文介绍了如何有效地从双数组复制到字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理一个包含超过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 the Buffer 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 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.


i would prefer Buffer.BlockCopy check this link


这篇关于如何有效地从双数组复制到字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆