0

I am working on a Project in which the I am using vectors since the size of array is unknown initially. Does using arrays instead of vectors reduce the run time of code?If Yes, Then How can i initialize/declare the array of unknown size i.e. Size of array is variable(based on input)? "OR" Is it better to use vectors only?

Note :- I want to know Which better reduces the Execution time of Program.

Veer
  • 211
  • 4
  • 14
  • Are you referring to `std::array`? Please provide some a bit more detail, preferably with a small code example. – skratchi.at May 08 '19 at 05:26
  • 1
    `std::vector` is just the right hammer if the size of array unknown at compile time is the nail. `std::vector` stores contents contiguously just like an array. Re-allocations can be prevented using `std::vector::resize()` or `std::vector::reserve()` as soon as the size is known at runtime. IMHO, your concerns are unnecessary. – Scheff's Cat May 08 '19 at 05:26
  • @skratchi.at I am referring to usual arrays like the ones we use in C. anything is fine if it reduces the runtime of program – Veer May 08 '19 at 05:32
  • @Scheff I have read that link But there he doesn't mention which better reduces the runtime of program? He just gave that Vectors are good to use because it is dynamic array – Veer May 08 '19 at 05:34
  • @Veer is the execution time really your problem or do you just want to know because you are curios? – skratchi.at May 08 '19 at 05:36
  • 1
    There are a lot of answers. I had a glance at the four highest voted. One of them mentions what I told above. Please, have an eye on what is told about "Micro Optimizers" - IMHO very worth to think about (OK - it reflects my own opinion.) ;-) – Scheff's Cat May 08 '19 at 05:36
  • @skratchi.at Yeah I want to reduce the execution time.However I am working on other things in my projects to reduce it, but If using arrays rather than Vectors reduces execution i will have to work on that also – Veer May 08 '19 at 05:39
  • 1
    Concerning access, a `std::vector` should be competitive to a `std::array` as the `std::vector::operator[]()` is just a "thin wrapper". (That can be used for read and write access just like an array.) In debug mode, it supports also a range check (what an array usually doesn't) which is very valuable. The only extra cost is the allocation but allocation of a dynamic array with `new[]` would have it as well. – Scheff's Cat May 08 '19 at 05:40
  • And my general rule of thumb for optimization (once learnt in a book I forgot the title of): Optimize the most inner loop and ignore all the outer loops. (At this time, the compilers were far away from what modern compilers can optimize themselves.) – Scheff's Cat May 08 '19 at 05:42
  • @Scheff Then either i use Vectors or array is the same means using either of them takes the same execution time as per your explanation. – Veer May 08 '19 at 05:44
  • I wouldn't sell my grandma for this, as it's compiler and std lib. dependent. However, for my daily business I assume that. Concerning arrays/vectors, cache locality is something worth to be considered. Have a look at the last part of my answer to [SO: Multi-threading benchmarking issues](https://stackoverflow.com/a/52835213/7478597) where I got measurement results that I even didn't expect myself. – Scheff's Cat May 08 '19 at 05:48

1 Answers1

2

In programming as in life there is no free meal... Keep that in mind all the time. If you want nice and convenient features you have to pay a price.

std::vector will add some complexity to your code you don't see. Adding items to your std::vector does more, then just writing a value. It may allocate new memory and copy the old values to it. And several more things you won't really see.

Switching to std::array won't give you the boost you might looking for. It is a bit simpler then std::vector. It is the way to got, when you are looking for an supplement of an plain c array. But still, it will add complexity too.

So my advice is and you will find similar once in good books. Try to optimize your code on algorithm base and not on the implementation. There is much more potential in possible flawed algorithms or there may be much better once. The implementation won't give you the ground braking boost.

skratchi.at
  • 1,151
  • 7
  • 22
  • 1
    I'd like to add that `std::vector` is probably as efficient as you could reasonably implement a "dynamic array" yourself. So there is usually no reason to worry about the performance. – Aykhan Hagverdili May 08 '19 at 06:32
  • 1
    True that. Self implemented versions won't probably reach the performance of the standard library. So the suggestion is, use the well tested and highly optimized once. – skratchi.at May 08 '19 at 06:39