简体   繁体   English

复制数组的最快方法是什么?

[英]What is the fastest way to copy my array?

I'm doing some Wave file handling and have them read from disk into an array of bytes. 我正在做一些Wave文件处理,让它们从磁盘读取到一个字节数组。 I want to quickly copy portions from this byte array into another buffer for intermediate processing. 我想快速将部分从这个字节数组复制到另一个缓冲区进行中间处理。 Currently I use something like this: 目前我使用这样的东西:

float[] fin;
byte[] buf;
//fill buf code omitted
for(int i=offset; i < size; i++){
  fin[i-offset] = (float) buf[i];  
} 

I feel that this is a slow method, because there is as much computation going on in the for loop conditional and increment as there is over in the actual body. 我觉得这是一个缓慢的方法,因为在for循环条件和增量中有尽可能多的计算在实际正文中有结束。 If there was a block copy avaliable in C# or some other way I can implement a block copy, that would be great. 如果在C#中有可用的块复制或其他方式我可以实现块复制,那将是很好的。

Maybe it isn't too slow, but it sure looks like a lot of work to move some data over. 也许它不是太慢,但确定移动一些数据看起来确实很多。 Here "size" is between 2^10 and 2^14. 这里“大小”介于2 ^ 10和2 ^ 14之间。 I am then handing the "fin" off to a FFT library, so this is by no means the slowest part of the code, maybe I'm barking up the wrong tree. 然后我将“fin”关闭到FFT库,所以这绝不是代码中最慢的部分,也许我正在咆哮错误的树。

EDIT UPDATE: I realize that micro optimizations are not where someone should spend their time, and I realize that profiling is a better way to achieve speedups overall, but I know that this code is in a 'hot path' and must be completed in under a third of a second on varying end user architectures to minimize our hardware system requirements. 编辑更新:我意识到微优化不是别人应该花时间的地方,我意识到分析是一种更好的方法来实现整体加速,但我知道这个代码是在一个'热门路径',必须在下面完成三分之一秒的不同终端用户体系结构,以最大限度地减少我们的硬件系统要求。 Even though I know that the following FFT code will be much more time consuming, I am looking for speedups where I can get them. 即使我知道以下FFT代码会耗费更多时间,但我正在寻找能够获得它们的加速。

Array.Copy sure looks nice, I didn't know about that before, and I consider this Q&A a success already! Array.Copy肯定看起来不错,之前我不知道,我认为这个Q&A已经成功了!

There is also: 还有:

Array.Copy
Array.CopyTo

but whether these will be faster will require profiling. 但这些是否更快将需要分析。

But be warned about focusing on micro-optimisations to the extent you miss the big picture, on modern PCs the effect of multi-level memory caching is likely to be greater than one approach or another to the copy. 但是要注意关注微观优化到你错过大局的程度,在现代PC上,多级内存缓存的效果可能比复制中的一种或另一种方法更大。

Edit: Quick check in reflector: both of the above methods boil down to a common native implementation (good). 编辑:快速检查反射器:上述两种方法归结为一个常见的本机实现(好)。

Note the docs for Array.Copy cover valid type conversions, a value -> value widening conversion like byte to float should be OK. 注意Array.Copy的文档覆盖有效类型转换,值 - >值扩展转换,如byte to float应该没问题。

看看Array.Copy它应该更快

Since you are converting from byte to float you are not going to get any significant speedup. 由于您要从byte转换为float,因此您不会获得任何显着的加速。 No Array.Copy or variation of memcopy can cope with that. 没有Array.Copy或memcopy的变体可以解决这个问题。

The only possible gain would be to 'poke' the byte value into a float. 唯一可能的收益是将字节值“戳”到浮点数中。 I don't know enough (about the implementation of float) to know if it will work and I honestly don't want to know either. 我不知道(关于float的实现)知道它是否会起作用我真的不想知道。

I won't reference knuth but profile your code. 我不会引用knuth但是会对您的代码进行分析。 Put some timestamps in and measure how long things are taking. 加上一些时间戳并测量事情的持续时间。 Then you can spend your time in optimization well :) 然后你可以把时间花在优化上:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM