Skip to main content
added 4 characters in body
Source Link
Glenn Teitelbaum
  • 10.4k
  • 4
  • 42
  • 82

For situations where 0s are common it would be faster to check 64 bytes at a time, and only check the bytes if the span is non-zero. If zero's are rare this will be more expensive. This code assumes that the large block is divisible by 64. This also assumes that memcmp is as efficient as you can get.

int countZeroBytes(byte[] values, int length) { static const byte zeros[64]={}; int zeroCount = 0; for (int i = 0; i < length; i+=64) { if (::memcmp(values+i, zeros, 64) == 0) { zeroCount += 64; } else { for (int j=i; j < i+64; ++j) { if (!values[j]) { ++zeroCount; } } } } return zeroCount; } 

For situations where 0s are common it would be faster to check 64 bytes at a time, and only check the bytes if the span is non-zero. If zero's are rare this will be more expensive. This code assumes that the large block is divisible by 64. This also assumes that memcmp is as efficient as you can get.

int countZeroBytes(byte[] values, int length) { static const byte zeros[64]={}; int zeroCount = 0; for (int i = 0; i < length; i+=64) { if (::memcmp(values+i, zeros, 64) == 0) { zeroCount += 64; } else { for (j=i; j < i+64; ++j) { if (!values[j]) { ++zeroCount; } } } } return zeroCount; } 

For situations where 0s are common it would be faster to check 64 bytes at a time, and only check the bytes if the span is non-zero. If zero's are rare this will be more expensive. This code assumes that the large block is divisible by 64. This also assumes that memcmp is as efficient as you can get.

int countZeroBytes(byte[] values, int length) { static const byte zeros[64]={}; int zeroCount = 0; for (int i = 0; i < length; i+=64) { if (::memcmp(values+i, zeros, 64) == 0) { zeroCount += 64; } else { for (int j=i; j < i+64; ++j) { if (!values[j]) { ++zeroCount; } } } } return zeroCount; } 
Source Link
Glenn Teitelbaum
  • 10.4k
  • 4
  • 42
  • 82

For situations where 0s are common it would be faster to check 64 bytes at a time, and only check the bytes if the span is non-zero. If zero's are rare this will be more expensive. This code assumes that the large block is divisible by 64. This also assumes that memcmp is as efficient as you can get.

int countZeroBytes(byte[] values, int length) { static const byte zeros[64]={}; int zeroCount = 0; for (int i = 0; i < length; i+=64) { if (::memcmp(values+i, zeros, 64) == 0) { zeroCount += 64; } else { for (j=i; j < i+64; ++j) { if (!values[j]) { ++zeroCount; } } } } return zeroCount; }