What is the fastest way to copy my array?

I am doing Wave data processing and reading them from disk to an array of bytes. I want to quickly copy parts from this byte array to another buffer for intermediate processing. I am currently using 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 in a for loop and as much computation takes place as much as in the actual body. If there is any copy of the block in C #, or I can implement a block copy, that would be great.

It may not be too slow, but it seems to work very hard to move some data. Here the "size" is between 2 ^ 10 and 2 ^ 14. Then I pass the fin to the FFT library, so this is not the slowest part of the code, maybe I bark the wrong tree.

EDIT UPDATE: I understand that micro optimization is not where someone should spend their time, and I understand that profiling is the best way to achieve acceleration in general, but I know that this code is in a “hot way” and should be less than a third full of second-to-one end-user architectures to minimize our system requirements. Although I know that the following FFT code will be much more time consuming, I am looking for accelerations where I can get them.

Array.Copy sure looks good, I did not know about it before, and I believe that this Q & A is already successful!

+4
source share
4 answers

There is also:

 Array.Copy Array.CopyTo 

but whether it will be faster, profiling will be required.

But be careful to focus on microoptimization until you miss the big picture, on modern PCs, the effect of multi-level caching of memory is likely to be greater than one or another approach to copying.

Edit: Quick reflector check: both of the above methods come down to a common native implementation (good).

Please note that the documents for Array.Copy arrays cover valid transformation types, the value → extension of the value extension, for example byte to float, should be in order.

+12
source

Look at Array.Copy, it should be faster

+3
source

Since you are switching from byte to float, you will not get significant acceleration. No Array.Copy or memcopy variations can handle this.

The only possible gain is to “push” the byte value into the float. I don't know enough (about the implementation of float) to know if this will work, and I honestly don't want to know that.

+3
source

I will not refer to knuth, but the profile of your code. Put some timestamps and measure how long this takes. Then you can have a good time in optimization :)

+1
source

All Articles