6

I know there are ways to either do or not do bounds-checking when indexing a vector, but specifically on push_back(), even if I know the capacity of the vector is large enough (ie., I reserved enough), and I ran a loop pushing back elements into it, I'd assume that since it's dynamically resizing it would always have to do bounds-checking (or size checking) on every push_back.

If this is the case, then I was thinking something like a fast_push() would be useful if you can know you won't exceed the capacity.

I've heard claims of some vector libraries being faster, such as this one http://andreoffringa.org/?q=uvector , but I didn't see specifically the issue of the push_back() when knowing bounds checking won't be needed. The one in the link makes claims of up to 50% speed improvements. One of them being preventing value initialisation on construction when you don't need it, and some other things.

Thanks.

Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • If you call `reserve()`, the compiler can usually optimize the `push_back()` really well. Standard library writers and compiler writers really try to optimize vector – Justin Jul 25 '17 at 21:51
  • 9
    Checking that there is enough room in the vector is just comparing two numbers: the current upper bound and the current size. It's not a bottleneck in your code. – Pete Becker Jul 25 '17 at 21:52
  • FWIW you can take vector implementation from the standard library, make some re-naming, add your fast_push function and realize that it does not deliver any significant performance gain :) – iehrlich Jul 25 '17 at 21:55
  • 1
    With what object do you instantiate the vector? Maybe you can go `resize` the vector beforehand, and use `[]` – geza Jul 25 '17 at 22:02
  • 1
    @PeteBecker: It may not be the bottleneck. But in certain situations, unnecessary checking does make a program slower. Even if it is just a simple integer compare. – geza Jul 25 '17 at 22:05
  • if it had significant perf impact then i would have thought that the STL designers would have allowed a naked_push. I think this is a good question though – pm100 Jul 25 '17 at 22:06
  • 1
    @pm100: in very small loops, it could have a significant perf impact. STL is a general library, not equally good for everything. I could tell you examples, where using STL has a **very** significant performace drop. – geza Jul 25 '17 at 22:09
  • The one in the link implements a **different** specification. – Pete Becker Jul 25 '17 at 22:12
  • If you're really concerned about maximum possible performance, maximum customization, then you should create your own library. In general, with `std::vector`, it is not possible what you asking. – geza Jul 25 '17 at 22:21
  • Thanks for your help. I get everyone's point about the impact being negligible, I was just thinking, you know, in big loops it IS an unnecessary check. I was creating 3D terrain meshes, pushing back a lot of vertices. Yes, I get std:: isn't necessarily ideal for performance goals. – Zebrafish Jul 25 '17 at 22:25
  • 2
    3D matrix as in vector>>? If so, your enemy is probably going to turn out to be poor spatial locality. A `vector` is contiguous, but there is no such guarantee for `vector`s of `vector`s, so a not-insignificant amount of time will be spent hopping from `vector` to `vector` and waiting around for the cache to load. – user4581301 Jul 25 '17 at 23:33
  • Yeah I thought about. A 3D std::vector is always essentially looking up the address of the buffer pointer, which is in three different places. I guess a 3-dimensional C array is one contiguous string of memory, and has an advantage. I'd imagine working with raw C arrays would be a lot harder though. I've wanted to try to create a voxel based game, but just thinking of the 3-dimensional array makes my head spin. – Zebrafish Jul 25 '17 at 23:54
  • 4
    to expand @user4581301's point: wrap your `std::vector` in a `class Matrix` and do the `(i * rows) + (k * cols) + j` calculation in a method – Caleth Jul 26 '17 at 08:18
  • 2
    If you can restructure your code to use ranged-`insert` instead of `push_back` and supply random-access iterators then I would expect a single bounds check rather than one per element as a basic QoI matter. – ildjarn May 31 '19 at 18:22
  • you can resize the vector before the cycle and then insert the elements in the corresponding position – Mandy007 Feb 12 '20 at 17:58

0 Answers0