开发者

Setting all array elements to an integer

I have an array,

int a[size];

I want to set all the array elements to 1

because some of the indexes in the array are already set to 1 so would it better checking each element using a conditional statement like,

for (int index = 0; index < size; index++)
{
    if (a[index] != 1)
      a[index] = 1;
开发者_C百科}

or set all the indexes no matter what. what would be the difference?


Your code has two paths in the loop, depending on each value:

  1. Read from array, comparison, and branch
  2. Read from array, comparison, and write

That's not worth it. Just write.

If you want, you can do the same by calling

std::fill(a, a + size, 1);

If the array is of type char instead of int, it will likely call memset. And platform-specific implementations of fill can offer the compiler optimization hints.


Just set all the elements to 1. Code for simplicity and readability first. If you find that the code runs too slow, profile it to see where improvements need to be made (although I highly doubt performance problems can come from setting elements of an array of integers to a certain value).


I'm guessing you are just looking for understanding and not battling a real performance issue... this just wouldn't show up under measurement and here's why:

Normally whenever a cached memory processor (i.e. most of today's desktop CPUs) has to write a value to memory, the cache line that contains the address must be read from (relatively slow) RAM. The value is then modified by a CPU write to the cache. The entire cache line is eventually written back to main RAM.

When you are performing operations over a range of continuous addresses like your array, the CPU will be able to perform several operations very quickly over one cache line before it is written back. It then moves on to the next cache line which was previously fetched in anticipation.

Most likely performing the test before writing the value will not be measurably different than just writing for several reasons:

  1. Branch prediction makes this process extremely efficient.
  2. The compiler will have done some really powerful optimizations.
  3. The memory transfer to cache RAM will be the real rate determining step.

So just write your code for clarity. Measure the difference if you are still curious.


Use an std::vector instead.

#include <vector>
...
std::vector<int> a(10, 1);

// access elements just as you would with a C array

std::cout << "Second element is: " << a[1] << std::endl;

Now you have an array of 10 integers all set to 1. If you already have an initialised vector, i.e. a vector filled with values other than one, you can use fill, like this:

#include <algorithm>
...
std::fill(a.begin(), a.end(), 1);


I wouldn't expect there to be a noticeable difference unless size is a very large value - however, if you're wanting the optimal variant then just setting all values to 1 would be the more performant option - I'm certain that the conditional will take more time than a simple assignment even if the assignment is then deemed not needed.


With C++11, you can use a the range-based for to set all values:

int a[size];
for(auto &v: a) {
    v = 1;
}

The &v iterates by reference, so the loop variable is assignable.

This format is a nice alternative to std::fill, and really comes into its own if there if the assignment is a more complicated expression.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜