0

I have a large integer array as below in my code which will hold pixel data. After some internal operations i need to check whether all the elements in the array are '0' or i have some valid values. Currently my code is running on the loop to verify it.

int a[1000000];

Is there any best to check for all zero values in array?

OS: Linux

Please suggest me solutions in memory efficient and time efficient.

Thanks, Parthiban N

Parthiban
  • 2,130
  • 2
  • 14
  • 27
  • 1
    Anything you do is going to be equivalent to your loop. – Keith Thompson Jul 31 '14 at 20:01
  • For arrays of `int` I find that to be more or less the case (on x86_64)... I get a 10% improvement by scanning by in units of `intmax_t` (and dealing with the alignment issues). Scanning an array of `char`, however, can be be improved by 70% (over 3 times faster) by scanning by `intmax_t`. The fastest method is by `memcmp()`, but only by 3%. Curiously, doing a raw `repz scasq` is slower than `memcmp()` and slower than scanning by `intmax_t`. –  Aug 01 '14 at 17:40
  • Actually, with a little unrolling, I find that scanning by `intmax_t` can save 20% over simply stepping down the array of `int`. (And save 74% for arrays of `char`... so ~4 faster.) –  Aug 01 '14 at 18:43
  • @gmch: Thanks for your answer. Can you please share your works done in regards? How the benchmarks are carried? – Parthiban Aug 03 '14 at 18:41

1 Answers1

1

Iterating over your array is just fine; If you have a look at the link sharth posted, the top answer mentions SIMD instructions.

You mention you are on linux, are you compiling for an x86 flavour platform, or something else? What C compiler are you using, and what compilation options? Both GCC and clang provide auto vectorisation, to varying degrees of performance.

In short, this is about as fast as it is going to get code wise, you are likely going to be able to squeeze more performance out by tuning your final executable with compilation directives, however.

bfoster
  • 79
  • 4
  • My end target will be on ARM. But i am checking with x86 for better understanding. Am using GCC with "-Wall -Wextra -O3" options. – Parthiban Aug 04 '14 at 07:11