If an array of bytes, which is basically zero, being a sparse array, you can use a 32-bit processor, making comparisons of 4 bytes at a time. Actual comparisons are performed 4 bytes at a time, however, if any of the bytes is non-zero, then you must determine which of the bytes in the unsigned long is non-zero, so more effort will be required. If the array is indeed sparse, then the time saved by comparisons can compensate for the extra work that determines which of the bytes is non-zero.
The easiest way would be to make an unsigned char array of no more than 4 bytes in size, so you donβt have to worry about making the last few bytes after the loop ends.
I would suggest doing a time study, because it is purely hypothetical, and there will be a point at which the array becomes insufficiently sparse to take longer than a simple loop.
One question I need is what you are doing with the displacement vector of nonzero elements of the array and whether you can end this vector. Another question: do you need a vector, is it possible to build a vector when you put elements in an array.
unsigned char* array=new unsigned char[4000000]; ...... unsigned long *pUlaw = (unsigned long *)array; for ( ; pUlaw < array + 4000000; pUlaw++) { if (*pUlaw) {
source share