-3

In the normal way I will use:

int A[100];
for (int i = 0; i < 100; i++){
    A[i] = i+1;
}

But have any other way to do that more efficient(running time)?

  • 1
    why searching the most efficient way for a very small code ? – Temani Afif Oct 03 '17 at 08:57
  • 9
    You don't say what dimension it is that you want to be "efficient" in, which makes this impossible to answer. Execution speed? Code size? Run-time memory use? Programmer understanding time? Development time? These are all trade-offs that you need to do. – unwind Oct 03 '17 at 08:57
  • 1
    [`std::iota`](http://en.cppreference.com/w/cpp/algorithm/iota)? But then again, please define what you mean by efficient? – Rakete1111 Oct 03 '17 at 08:59
  • I'm curious: why do you need it? – juanchopanza Oct 03 '17 at 09:01
  • 2
    If the values are not going to change, i.e. if the initial values are always going to be `{1,2,3,4,......100}` then instead of calling it in a loop just hardcode the values. Because looping will cause branch and jump instruction which is a little bit time consuming, but now a days compilers are smart if compiler come across any such statement like in your example and if proper optimization is chosen then it will do the loop unrolling for you. – Gaurav Pathak Oct 03 '17 at 09:06
  • The code you wrote is already quite efficient? What do you expect? Why do you need this to be more efficient? Efficient in terms of what? What problem are you _actually_ trying to resolve. Give us a broader picture. Read this : [XY Problem](http://xyproblem.info/) – Jabberwocky Oct 03 '17 at 09:11
  • Also, C or C++ or both? – CinCout Oct 03 '17 at 09:24
  • So how to make it more efficient in runing time. – Thừa Nguyễn Oct 03 '17 at 09:32
  • 1
    If you are interested in a compile time solution, look at this: https://stackoverflow.com/questions/41660062/how-to-construct-an-stdarray-with-index-sequence. – Bob__ Oct 03 '17 at 12:47

1 Answers1

4

In C++, the most effective (my chosen measure of efficiency is developer effort to produce and maintain) way to start with such problems is to try to find a standard algorithm to fit the bill. In this case, a suitable algorithm is std::iota() (in standard header <numeric> since C++11).

 std::vector<int> A(100);
 std::iota(A.begin(), A.end(), 1);    // alternative: std::iota(std::begin(A), std::end(A), 1);

or (if you insist on using an array)

 int A[100]
 std::iota(std::begin(A), std::end(A), 1);

Before C++11, a use of std::generate() with an appropriate functor will work. As noted by interjay in comments, that may not be most efficient in terms of developer effort (over, say, a simple loop). However, whipping up an appropriate functor - a struct type with a suitable constructor, data member, and operator() - can become second nature once one is used to the notion of using facilities in the standard library.

In C, a simple loop is probably fine, at least in the first instance (i.e. unless you have specific measurements providing evidence that "inefficiency", however, you define that, is excessive).

Peter
  • 35,646
  • 4
  • 32
  • 74
  • 1
    Adding a dynamic allocation + needless zero initialization is unlikely to be more "efficient", and there is no need for it. – juanchopanza Oct 03 '17 at 09:08
  • 4
    @juanchopanza - my chosen measure of efficiency is developer effort. – Peter Oct 03 '17 at 09:09
  • 3
    In pre-c++11,`std::generate()` with a functor would take a lot more developer effort than a simple `for` loop. – interjay Oct 03 '17 at 09:09
  • The dynamic allocation, plus the zero initialization from using `std::vector` doesn't reduce developer effort. – juanchopanza Oct 03 '17 at 09:11
  • @interjay - that's a matter of opinion. A `struct` type with a simple constructor and an `operator()` can be done pretty easily, particularly by people who are familiar with thinking in terms of using the standard library. But fair point that it will take more effort for people not used to thinking in that way. – Peter Oct 03 '17 at 09:13
  • @Peter You'd need a struct with an integer member, a constructor that initializes it, an `operator()` that increases and returns it, and then a `generate` call. In total about 8-10 lines of code. As opposed to the two lines of a `for` loop. – interjay Oct 03 '17 at 09:24
  • Lines of code is actually a very poor measure of developer effort. – Peter Oct 03 '17 at 09:25
  • 1
    This argument is leading nowhere, some people find some ways of coding easier, it is a matter of opinion (and also why the question isn't a good one) – Passer By Oct 03 '17 at 09:25
  • @PasserBy - agreed – Peter Oct 03 '17 at 09:26