0

I have multiple same length arrays I wish to fill with zero's. Let's look at two ways of doing that:

1)

int i;
for(i=0;i<ARRAYSLENGTH;i++){
    arr1[i]=0;
    arr2[i]=0;
    arr3[i]=0;
    ...
}

2) memset all the arrays to zero.

In a recent code review I was asked to change option 1 to option 2. Which made me wonder, Which of these methods is better ? Mainly:

Is 2 considered more readable than 1?

How do these two methods compare in terms of efficiency ? (considering memset is usually implemented in assembly but method 1 only increments the counter once for multiple arrays).

5
  • 2
    Subjectively, yes, 2 is more readable and expresses your intent more clearly. Also, you probably realise this, but memset has some tricks up its sleeve that can make it faster than the naive one-by-one approach. Commented Dec 28, 2012 at 14:41
  • 2
    There is a third approach A arr1[10] ={0}; It will set all the 10 elements to 0. Commented Dec 28, 2012 at 14:42
  • If you combine them all into one two-dimensional array, you can get away with one call to memset or the simpler (IMO) A arr[NUM_ARRAYS][ARRAY_LENGTH] = {{0}};. Commented Dec 28, 2012 at 14:54
  • Note, though, that memset will only work when initializing with a constant stream of bytes while the initialization syntax will only work when all unmentioned values are to be set to 0. If/When you want to initialize to something else, the for loop will be your best bet. Commented Dec 28, 2012 at 14:58
  • Also, compilers have been known to optimize such for loops into calls to memset, so there may be no performance advantage whatsoever. In that case you should prefer whichever version is clearer. Commented Dec 28, 2012 at 15:00

3 Answers 3

5

Your method 1 (the for loop), is bad for the cache.

As arr1, arr2, arr3 may not be anywhere near each other in memory, and very likely will not be in the cache together, you could have frequent cache-misses, and the CPU will have to constantly fetch new pieces from memory, just to set them to zero.

By doing a set of memset operations, you'll hit ALL of arr1, at once, almost certainly entirely from the cache. Then you'll cache and set all of arr2 very quickly, etc, etc.

That, and because memset may well have assembly tricks and optimizations to make it faster, I would definitely prefer option 2 over option 1.

Sign up to request clarification or add additional context in comments.

Comments

0

I think most programmers should be able to read memset and know what it's doing so readability shouldn't be an issue. With most modern compilers you probably wouldn't see much performance difference but I would use memset as that's what it's for.

1 Comment

I'd say that memset is more readable, because you don't have to read much to know what the code is doing. Having to read a for loop and understand what it's doing is more taxing (even if it's a simple loop).
0

You can even use bzero instead of memset which is more specific

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.